我在测试类中遇到错误实现K和类似的错误。它说“类型参数K不在类型变量K的范围内:
二进制搜索树类:
public class BinarySearchTree<K extends Comparable<K>> implements Tree<K> {
public Node(K data, Node left, Node right) {
.....
}
测试类:
private <K> int get_height(BinarySearchTree<K>.Node p) {
.......
}
我以为我需要把它改成......
private <K> int get_height(BinarySearchTree<K extends Comparable<K>>.Node p) {
.......
}
然而,这会导致诸如“意外绑定”之类的错误
答案 0 :(得分:1)
BinarySearchTree
预计K
extends Comparable<K>
。您可以在声明泛型方法get_height
的类型参数时指定此项:
private <K extends Comparable<K>> int get_height(BinarySearchTree<K>.Node p) {...}