IUn*_*own 2 java generics comparable
我遇到了泛型类的以下定义:
public class binarysearchnode<T extends Comparable<T>> implements Comparable<binarysearchnode<T>>{
.............
}
Run Code Online (Sandbox Code Playgroud)
请帮助解释为什么类在实现类似的接口时将自己指定为Type参数以进行比较?它与以下内容有何不同:
public class binarysearchnode<T extends Comparable<T>> implements Comparable<? super (or extends)T>{
.............
}
Run Code Online (Sandbox Code Playgroud)
这使得可以将binarysearchnodes彼此进行比较.如果它实现了Comparable<T>,那意味着可以将节点与节点的值进行比较,这将是奇数.
在课堂上你可能会发现这样的事情:
T value;
public int compareTo(binarysearchnode<T> other) {
return value.compareTo(other.value);
}
Run Code Online (Sandbox Code Playgroud)
为了能够compareTo()像这样实现,值class(T)需要与其类的其他对象相比 - 因此<T extends Comparable<T>>在类定义中声明.