[英]Java generic multiple bounds error:cannot be inherited with different type arguments
When I wrote the code, I thought about using generic multiple bounds to achieve a problem,but when I finished writing, it kept reporting errors:cannot be inherited with different type arguments写代码的时候想过用泛型多重边界来实现一个问题,但是写完就一直报错:cannot beinherited with different type arguments
I have the following classes:我有以下课程:
public class Animal implements Comparable<Animal> {}
public class Dog extends Animal {}
I also defined a static method:我还定义了一个 static 方法:
public static <T extends Animal & Comparable<? extends T>> T getMin(List<T> value) { return null;}
My idea is to define a generic type, this generic type must be a subclass of Animal, and this generic type must implement the Comparable interface, and the actual parameters of the interface are Annimal and its subclass class.我的想法是定义一个泛型,这个泛型必须是Animal的子类,而且这个泛型必须实现Comparable接口,接口的实际参数是Animal及其子类class。
Later I made another attempt,defined another static method:后来我又做了一次尝试,定义了另一个 static 方法:
public static <T extends Animal & Comparable<? super T>> T getMin(List<T> value) { return null;}
My idea is to define a generic type, this generic type must be a subclass of Animal, and this generic type must implement the Comparable interface, and the actual parameters of the interface are Annimal and its parent class.我的想法是定义一个泛型,这个泛型必须是Animal的子类,而且这个泛型必须实现Comparable接口,接口的实际参数是Animal及其父class。
It is frustrating that both methods report errors: java.lang.Comparable' cannot be inherited with different type arguments: 'Animal' and 'capture令人沮丧的是两种方法都报错: java.lang.Comparable' cannot be inherit with different type arguments: 'Animal' and 'capture
Finally, I found that the Animal and dog classes need to be modified as follows,just like number and its subclasses:最后,我发现 Animal 和 dog 类需要修改如下,就像 number 及其子类一样:
public class Animal {}
public class Dog extends Animal implements Comparable<Dog> {}
But I do n't understand why it needs to be modified like this, and why the error was reported at the beginning.但是不明白为什么要这样修改,为什么一开始就报错。
Java's generics are only known by the Java compiler, but not the JVM. Java 的 generics 只有 Java 编译器知道,但 JVM 不知道。 This is known as type erasure .这称为类型擦除。 Comparable<Animal>
and Comparable<Dog>
are the same type, as far as the JVM is concerned.就 JVM 而言, Comparable<Animal>
和Comparable<Dog>
是同一类型。 The JVM just thinks of both of them as Comparable
. JVM 只是认为它们都是Comparable
。
This is invalid:这是无效的:
<T extends Animal & Comparable<? extends T>>
Because you are saying that T
should implement two interfaces at the same time: Comparable<Animal>
(since Animal
implements Comparable<Animal>
) and Comparable<U>
where U
is a subclass of T
.因为您说T
应该同时实现两个接口: Comparable<Animal>
(因为Animal
实现了Comparable<Animal>
)和Comparable<U>
其中U
是T
的子类。 T
can't implement these two interfaces at the same time, because although they look different to the compiler, the runtime thinks they are the same Comparable
. T
不能同时实现这两个接口,因为虽然它们在编译器看来不同,但运行时认为它们是相同的Comparable
。
This is the same reason why you can't have Animal
implement Comparable<Animal>
and Dog
implement Comparable<Dog>
.这就是为什么不能让Animal
实现Comparable<Animal>
和Dog
实现Comparable<Dog>
的原因。 Dog
would be implementing two interfaces that look the same to the runtime! Dog
将实现两个在运行时看起来相同的接口!
You could say:你可以说:
<T extends Animal & Comparable<Animal>>
but that's kind of redundant...但这有点多余...
By making only Dog
implement Comparable<Dog>
, T extends Animal
no longer implies T implements Comparable<Animal>
, so you are no longer saying that T
should implement 2 different interfaces.通过仅使Dog
实现Comparable<Dog>
, T extends Animal
不再意味着T implements Comparable<Animal>
,因此您不再说T
应该实现 2 个不同的接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.