简体   繁体   English

Java 泛型多重边界错误:不能用不同类型继承 arguments

[英]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>其中UT的子类。 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.

相关问题 Java 不能用不同的参数继承 - Java cannot be inherited with different arguments java突变设计模式和编译器错误“接口”不能用不同的类型参数“ TypeA”和“ TypeB”继承 - java mutant design pattern and compiler error 'Interface' cannot be inherited with different type arguments 'TypeA' and 'TypeB' 为什么不能用不同的类型继承 arguments - Why cannot be inherited with different type arguments 在Java返回类型中强制执行多个通用边界 - Enforcing Multiple Generic Bounds in Java Return Type ISomeBaseInterface不能使用不同的参数继承: <java.lang.Object> 和&lt;&gt; - ISomeBaseInterface cannot be inherited with different arguments: <java.lang.Object> and <> 错误“kafkaconsumer 类型不是通用的,不能用参数参数化” - Error “the type kafkaconsumer is not generic it cannot be parameterized with arguments” T型不是通用的; 它不能用参数参数化 <?> 通用函数中的错误 - The type T is not generic; it cannot be parameterized with arguments <?> error in a generic function Java通用接口“类型参数不在范围内”错误 - Java Generic Interface 'Type Argument Not Within Bounds' Error 如何解决“类型列表不是通用的;它不能用参数参数化<String> 《黄瓜硒JAVA中的错误》 - How to fix "The type List is not generic; it cannot be parameterized with arguments <String>" error in cucumber selenium JAVA FluentWait 类型不是通用的; 它不能用 arguments 参数化<webdriver> FluentWait 错误 Class 到 Selenium 和 Java</webdriver> - The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> error for FluentWait Class through Selenium and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM