我在测试类中收到错误,因为 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) {
.......
}
然而,这会导致诸如“意外绑定(bind)”之类的错误
最佳答案
BinarySearchTree
预计 K
其中extends Comparable<K>
。您可以在声明泛型方法的类型参数时指定此参数 get_height
:
private <K extends Comparable<K>> int get_height(BinarySearchTree<K>.Node p) {...}
关于java - 类型参数 K 不在类型变量 K 的范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46394260/