先看代码:
/**
* 父类
*/
public class a<T extends Comparable<T>> {
protected T t1;
protected T t2;
protected ArrayList<T> list;
public a() {
t1.compareTo(t2); //泛型继承了Comparable,便可调用compareTo方法
T t = list.get(0); //list里的元素是T型
}
}
之后要用另一个类继承这个类 语法不会写了=。= 下面是错误的代码
public class b<T> extends a<Comparable<T>>{ //error
public b() {
t1.compareTo(t2); //error
T t = list.get(0); //error
}
}
上面的程序会报错:
Bound mismatch: The type Comparable<T> is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type a<T>
The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Comparable<T>)
Type mismatch: cannot convert from Comparable<T> to T
b类后面这个<T> 没有和a类的<T extends Comparable<T>> 关联上
求教应当怎么写T-T
------解决思路----------------------
楼主改为这样试试
public class B<T extends Comparable<T>> extends A<T> {
}