Class<? extends A & B>这样写为何不行,有什么替代方法
1
wolfie Oct 28, 2021
Java 不支持多继承
B 可以为 Interface |
2
77yf77yf77yf OP @wolfie 没说清楚不好意思,A 是一个抽象类,B 是一个接口,这样写上去 idea 会报错
|
3
hingbong Oct 28, 2021
<T extends Object & Serializable> void a(Class<T> t) {}
|
5
wolfie Oct 28, 2021
|
6
77yf77yf77yf OP @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 |
7
hingbong Oct 28, 2021 注解上不行
@interface members may not have type parameters 非注解可以 public interface CacheKey { <T extends Object & Comparable<? super T>> Class<T> keyGen(); } |
8
hingbong Oct 28, 2021
|
9
77yf77yf77yf OP @hingbong 那对于注解有什么替代方案吗,不然只能用反射检查?
|
10
hingbong Oct 28, 2021
我是写 kotlin 的
试了一下 kotlin 可以,但是那个注解到 Java 下又无法使用,只能在 kotlin 里用,Java 里我就不清楚了 |
11
77yf77yf77yf OP @hingbong 好的,谢谢了
|