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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:06:40  来源:igfitidea点击:

Java generics issue: Class "not within bounds of type-variable" error.

javagenericsbinary-treebinary-search-tree

提问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 MySearchTreethe Kof the base type is Course. So Kmust "extend" Comparable<Keyable<Course>> & Keyable<Course>. But it doesn't, it extends Comparable<Keyable<DataElement>> & Keyable<DataElement>.

MySearchTreeK基本类型的是Course。所以K必须“扩展” Comparable<Keyable<Course>> & Keyable<Course>。但它没有,它扩展了Comparable<Keyable<DataElement>> & Keyable<DataElement>

I guess DataElementshould be generified in a similar manner to Comparableor Enum.

我想DataElement应该以类似于Comparableor 的方式进行泛化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> {