我想定义一个实现一般Comparable接口的类.在我的类中,我还定义了一个通用类型的元素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
有人可以告诉我为什么和如何解决它?