77yf77yf77yf
V2EX  ›  Java

泛型限定问题

  •  
  •   77yf77yf77yf · Oct 28, 2021 · 2355 views
    This topic created in 1685 days ago, the information mentioned may be changed or developed.
    Class<? extends A & B>这样写为何不行,有什么替代方法
    11 replies    2021-10-28 17:36:45 +08:00
    wolfie
        1
    wolfie  
       Oct 28, 2021
    Java 不支持多继承
    B 可以为 Interface
    77yf77yf77yf
        2
    77yf77yf77yf  
    OP
       Oct 28, 2021
    @wolfie 没说清楚不好意思,A 是一个抽象类,B 是一个接口,这样写上去 idea 会报错
    hingbong
        3
    hingbong  
       Oct 28, 2021
    <T extends Object & Serializable> void a(Class<T> t) {}
    hingbong
        4
    hingbong  
       Oct 28, 2021
    @hingbong 我这么写没报错
    wolfie
        5
    wolfie  
       Oct 28, 2021
    @77yf77yf77yf #2
    检查 idea 配置 module sdk 、java compiler 版本。
    检查 抽象类 和 Interface 方法冲突
    77yf77yf77yf
        6
    77yf77yf77yf  
    OP
       Oct 28, 2021
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface CacheKey {
    Class<? extends AbstractStdCacheNaming & ICacheKeyNamingStrategy> keyGen() default EntityBasedKeyNamingStrategy.class;
    ...
    }

    public abstract class AbstractStdCacheNaming {...}

    public interface ICacheKeyNamingStrategy {...}


    Class<? extends AbstractStdCacheNaming & ICacheKeyNamingStrategy> keyGen() default EntityBasedKeyNamingStrategy.class;
    这一句就报错了

    @hingbong
    hingbong
        7
    hingbong  
       Oct 28, 2021   ❤️ 1
    注解上不行
    @interface members may not have type parameters
    非注解可以
    public interface CacheKey {
    <T extends Object & Comparable<? super T>> Class<T> keyGen();
    }
    hingbong
        8
    hingbong  
       Oct 28, 2021
    77yf77yf77yf
        9
    77yf77yf77yf  
    OP
       Oct 28, 2021
    @hingbong 那对于注解有什么替代方案吗,不然只能用反射检查?
    hingbong
        10
    hingbong  
       Oct 28, 2021
    我是写 kotlin 的
    试了一下 kotlin 可以,但是那个注解到 Java 下又无法使用,只能在 kotlin 里用,Java 里我就不清楚了
    77yf77yf77yf
        11
    77yf77yf77yf  
    OP
       Oct 28, 2021
    @hingbong 好的,谢谢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3187 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 14:16 · PVG 22:16 · LAX 07:16 · JFK 10:16
    ♥ Do have faith in what you're doing.