泛型类扩展其他泛型类的错误

时间:2014-09-19 13:14:52

标签: java generics

我遇到了泛型类的问题,我将用我的示例类来解释它:

二叉树

public class BinaryTree<T extends Comparable<T>> {

索引

public abstract class Indexed<T extends Comparable<T>> implements Comparable<Indexed<T>> {

IndexedBinaryTree

public class IndexedBinaryTree<T extends Indexed<? extends Comparable<Indexed<?>>>> extends BinaryTree<T> {

扩展索引

的示例类
public class Vokabel extends Indexed<String> {

IndexedBinaryTree不起作用。我想要做的是拥有一个包含索引对象的BinaryTree,可以像这样创建:

IndexedBinaryTree<Vokabel> ibt = new IndexedBinaryTree<>();

错误 NetBeans正在给我:

  

类型参数? extends Comparable&gt;不在范围内   类型变量T.         其中T是一个类型变量:           T扩展了在Indexed

类中声明的Comparable
> expected

type argument T#1 is not within bounds of type-variable T#2
  where T#1, T#2 are type-variables:
    T#1 extends Indexed<? extends Comparable<Indexed<?>>> declared in class IndexedBinaryTree
    T#2 extends Comparable<T#2> declared in class BinaryTree

1 个答案:

答案 0 :(得分:0)

编译:

class BinaryTree<T extends Comparable<? super T>> {}
abstract class Indexed<T extends Comparable<? super T>> implements Comparable<Indexed<T>> {}
class IndexedBinaryTree<T extends Indexed<?>> extends BinaryTree<T> {}