Java 泛型问题:类“不在类型变量的范围内"错误. [英] Java generics issue: Class "not within bounds of type-variable" error.
问题描述
我正在做一个涉及泛型的类项目.
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;
...
}
}
在 MySearchTree 的声明中尝试使用 Course 类时,我收到一个类型参数错误,指出Course 不在类型变量 K 的范围内".我花了很多时间试图弄清楚 Course 可能缺乏哪些属性使其不符合要求,但结果却是空洞的.
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
中,基本类型的K
是Course
.所以K
必须扩展"Comparable
.但它没有,它扩展了 Comparable
.
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>
.
我想 DataElement
应该以类似于 Comparable
或 的方式进行泛化枚举
.
I guess DataElement
should be generified in a similar manner to 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> {
这篇关于Java 泛型问题:类“不在类型变量的范围内"错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!