77yf77yf77yf
V2EX  ›  Java

泛型限定问题

  •  
  •   77yf77yf77yf · Oct 28, 2021 · 2712 views
    This topic created in 1833 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
    wolfie
    Oct 28, 2021
    1
    Java 不支持多继承
    B 可以为 Interface
    77yf77yf77yf
    77yf77yf77yf
    OP
    Oct 28, 2021
    2
    @wolfie 没说清楚不好意思,A 是一个抽象类,B 是一个接口,这样写上去 idea 会报错
    hingbong
    hingbong
    Oct 28, 2021
    3
    <T extends Object & Serializable> void a(Class<T> t) {}
    hingbong
    hingbong
    Oct 28, 2021
    4
    @hingbong 我这么写没报错
    wolfie
    wolfie
    Oct 28, 2021
    5
    @77yf77yf77yf #2
    检查 idea 配置 module sdk 、java compiler 版本。
    检查 抽象类 和 Interface 方法冲突
    77yf77yf77yf
    77yf77yf77yf
    OP
    Oct 28, 2021
    6
    @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
    hingbong
    Oct 28, 2021❤️1
    7
    注解上不行
    @interface members may not have type parameters
    非注解可以
    public interface CacheKey {
    <T extends Object & Comparable<? super T>> Class<T> keyGen();
    }
    hingbong
    hingbong
    Oct 28, 2021
    8
    77yf77yf77yf
    77yf77yf77yf
    OP
    Oct 28, 2021
    9
    @hingbong 那对于注解有什么替代方案吗,不然只能用反射检查?
    hingbong
    hingbong
    Oct 28, 2021
    10
    我是写 kotlin 的
    试了一下 kotlin 可以,但是那个注解到 Java 下又无法使用,只能在 kotlin 里用,Java 里我就不清楚了
    77yf77yf77yf
    77yf77yf77yf
    OP
    Oct 28, 2021
    11
    @hingbong 好的,谢谢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5656 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 03:10 · PVG 11:10 · LAX 19:10 · JFK 22:10
    ♥ Do have faith in what you're doing.