Java 泛型问题:类“不在类型变量范围内”错误。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9968687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java generics issue: Class "not within bounds of type-variable" error.
提问by SamWN
I'm working on a project for class that involves generics.
我正在研究一个涉及泛型的类项目。
public interface Keyable <T> {public String getKey();}
public interface DataElement extends Comparable<Keyable<DataElement>>, Keyable<DataElement>, Serializable {...}
public class Course implements DataElement {...}
public interface SearchTree<K extends Comparable<Keyable<K>> & Keyable<K>> extends Serializable {...}
public class MySearchTree implements SearchTree<Course> {
...
private class Node {
public Course data;
public Node left;
public Node right;
...
}
}
When trying to use the Course class within the declaration of MySearchTree, I receive a type argument error stating that "Course is not within the bounds of type-variable K". I spent a good amount of time trying to figure out what attributes Course might be lacking to make it not fit the bill, but came up empty.
当尝试在 MySearchTree 的声明中使用 Course 类时,我收到一个类型参数错误,指出“Course 不在类型变量 K 的范围内”。我花了很多时间试图弄清楚 Course 可能缺乏哪些属性使其不符合要求,但结果却是空洞的。
Any ideas?
有任何想法吗?
回答by Tom Hawtin - tackline
In MySearchTree
the K
of the base type is Course
. So K
must "extend" Comparable<Keyable<Course>> & Keyable<Course>
. But it doesn't, it extends Comparable<Keyable<DataElement>> & Keyable<DataElement>
.
在MySearchTree
该K
基本类型的是Course
。所以K
必须“扩展” Comparable<Keyable<Course>> & Keyable<Course>
。但它没有,它扩展了Comparable<Keyable<DataElement>> & Keyable<DataElement>
。
I guess DataElement
should be generified in a similar manner to Comparable
or Enum
.
我想DataElement
应该以类似于Comparable
or 的方式进行泛化Enum
。
public interface Keyable <T> {public String getKey();}
public interface DataElement<THIS extends DataElement<THIS>> extends Comparable<Keyable<THIS>>, Keyable<THIS>, Serializable {...}
public class Course implements DataElement<Course> {...}
public interface SearchTree<K extends Comparable<Keyable<K>> & Keyable<K>> extends Serializable {...}
public class MySearchTree implements SearchTree<Course> {