如何在Java中使用这些泛型创建对象? (<t extends =“”keyeditem <kt =“”>,KT扩展了Comparable <! - ?super KT - >&gt;)

时间:2018-04-30 17:39:28

标签: java generics instantiation

以下是我正在使用的课程......

public class MyBinarySearchTreePlus<T extends KeyedItem<KT>, KT extends Comparable<? super KT>>
        extends MyBinarySearchTree<T, KT> implements BSTPInterface<T, KT> {
    public MyBinarySearchTreePlus() {
        super();
    }
}

public class MyBinarySearchTree<T extends KeyedItem<KT>, KT extends Comparable<? super KT>>
            extends BinaryTreeBasis<T> {

    public MyBinarySearchTree() {}
}  

public abstract class BinaryTreeBasis<T> {
    protected TreeNode<T> root;

    public BinaryTreeBasis() {
      root = null;
    }
}

public abstract class KeyedItem<KT extends
                                 Comparable<? super KT>> {
    private KT searchKey;

    public KeyedItem(KT key) {
        searchKey = key;
    }
}

我尝试创建BinarySearchTree,我尝试:

MyBinarySearchTreePlus<Integer, Integer> tree = new MyBinarySearchTreePlus<Integer, Integer>();

我总是得到错误(来自第一个参数):

Bound mismatch: The type Integer is not a valid substitute for the bounded 
parameter <T extends KeyedItem<KT>> of the type MyBinarySearchTreePlus<T,KT>

什么样的对象是T扩展KeyedItem寻找?它不能与Comparable Objects,非Comparable Objects,原始对象一起编译。那么到底我应该在这里使用什么呢?我不明白为什么第一个地方甚至有2个,显然1是你在树上存储的类型但是其他1是什么?我也有一个类型为T的TreeNode类,如果这有任何区别的话。

0 个答案:

没有答案