作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这听起来很困惑,但这是我能解释的最好的了。 (您可以建议一个更好的标题)。我有 3 节课:-
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
时遇到错误
绑定(bind)不匹配:类型 C 不是类型 A 的有界参数
最佳答案
感谢上面@Boris the Spider 的评论
问题是 C
是 B
中的原始类型。更改实例化以包含参数(根据需要)
A< C<Integer> > var = new A< C<Integer> >();
编辑1:另外,感谢下面的评论。更好的做法是将 C
中的compareTo 方法更改为此,
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> >{...}
关于java - 绑定(bind)不匹配 : Generic class (of a Generic class extending Comparable (of a Generic class extending Comparable)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30019440/
我是一名优秀的程序员,十分优秀!