Bound mismatch: The type Integer is not a valid substitute for the bounded parameter <Item extends Comparable<Item>> of the type BTNode<Item>
这是我收到错误的地方:
public class BinaryTree<Integer> {
private BTNode<Integer> root;
//...
}
这是我正在使用的类:
public class BTNode<Item extends java.lang.Comparable<Item>> implements java.lang.Comparable<BTNode<Item>> {
private Item data;
//...
}
我认为Integer
包装器应满足条件 <xx extends Comparable<xx>>
。
我的理解有错吗?你能告诉我我在这里做错了什么吗?
仅供记录,我正在使用 java.lang.Comparable
而不是我自己的 Comparable
实现.
最佳答案
问题是这样的:
public class BinaryTree<Integer> {
private BTNode<Integer> root;
}
类参数名为 Integer
。声明BTNode<Integer>
时,您没有引用 java.lang.Integer
而是参数。
我真的不知道你想做什么,但有一个建议:最好使用一个字母来命名你的类参数。例如使用时不会出现错误:
public class BinaryTree<T> {
private BTNode<Integer> root;
}
如果你想要root
要真正使用类参数,那么:
public class BinaryTree<T extends Comparable<T>> {
private BTNode<T> root;
}
看看下面的类(class):http://docs.oracle.com/javase/tutorial/extra/generics/index.html
关于java - 使用 <Item extends java.lang.Comparable<Item>> 时绑定(bind)不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25115537/