泛型限定问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
77yf77yf77yf
V2EX    Java

泛型限定问题

  •  
  •   77yf77yf77yf Oct 28, 2021 2255 views
    This topic created in 1641 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     4007 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 05:09 PVG 13:09 LAX 22:09 JFK 01:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86