实现与泛型类的可比性
2022-09-01 20:44:46
我想定义一个实现泛型可比接口的类。在我的课堂上,我还定义了一个泛型类型元素。为了实现接口,我将比较委托给 。这是我的代码:T
T
public class Item<T extends Comparable<T>> implements Comparable<Item> {
private int s;
private T t;
public T getT() {
return t;
}
@Override
public int compareTo(Item o) {
return getT().compareTo(o.getT());
}
}
当我尝试编译它时,我得到以下错误信息:
Item.java:11: error: method compareTo in interface Comparable<T#2> cannot be applied to given types;
return getT().compareTo(o.getT());
^
required: T#1
found: Comparable
reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Comparable<T#1> declared in class Item
T#2 extends Object declared in interface Comparable
1 error
任何人都可以告诉我为什么以及如何解决它?