我知道这听起来很混乱,但这是我能解释的最好的了。(你可以建议一个更好的标题)。我有三门课:-
A
public class A <T extends Comparable<T>> {
...
}B
public class B {
A<C> var = new A<C>();
// Bound mismatch: The type C is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type A<T>
...
}C
public class C <T extends Comparable<T>> implements Comparable<C>{
private T t = null;
public C (T t){
this.t = t;
}
@Override
public int compareTo(C o) {
return t.compareTo((T) o.t);
}
...
}在尝试在B中实例化A时,我收到了一个错误
界不匹配:C类型不能有效地替代A类型的有界参数
发布于 2015-05-03 21:24:59
多亏了上面的评论,蜘蛛鲍里斯
问题是C是B中的原始类型。将实例化更改为包含参数(取决于需要)
A< C<Integer> > var = new A< C<Integer> >();编辑1:也要感谢下面的评论。更好的做法是将compareTo方法在C中更改为
public int compareTo(C<T> o) {
return t.compareTo(o.t);
}编辑2:同样,问题中有一个错误(w.r.t )。(以下评论)
public class C <T extends Comparable<T>> implements Comparable< C<T> >{...}https://stackoverflow.com/questions/30019440
复制相似问题