我的签名上应该有什么insert
-方法?我正在与仿制药作斗争。在某种程度上,我两者都想要Comparable<T>
and T
我尝试过<Comparable<T> extends T>
.
public class Node<T> {
private Comparable<T> value;
public Node(Comparable<T> val) {
this.value = val;
}
// WRONG signature - compareTo need an argument of type T
public void insert(Comparable<T> val) {
if(value.compareTo(val) > 0) {
new Node<T>(val);
}
}
public static void main(String[] args) {
Integer i4 = new Integer(4);
Integer i7 = new Integer(7);
Node<Integer> n4 = new Node<>(i4);
n4.insert(i7);
}
}
不确定您想要实现什么,但是您不应该将其包含在类的声明中吗?
public static class Node<T extends Comparable<T>> { //HERE
private T value;
public Node(T val) {
this.value = val;
}
public void insert(T val) {
if (value.compareTo(val) > 0) {
new Node<T>(val);
}
}
}
注意:使用是一个很好的做法<T extends Comparable<? super T>>
代替<T extends Comparable<T>>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)