JAVA:绑定不匹配:不是有界参数<e extends =“”的有效替代<e =“”>&gt;

时间:2016-01-12 07:07:53

标签: java generics collections tree binary-tree

我有BinaryTree类:

    public class BinaryTree<E extends Comparable<E>> extends AbstractTree<E> {
        protected TreeNode<E> root;
        protected int size = 0;
        private final Comparator<? super E> comparator;

        /** Create a default binary tree */
        public BinaryTree() {
            comparator = null;
        }
/** Create a binary tree from an array of objects */
    public BinaryTree(E[] objects) {
        for (int i = 0; i < objects.length; i++)
            insert(objects[i]);
    }
    public BinaryTree(E[] objects, Comparator<E> c) {
        for (int i = 0; i < objects.length; i++)
            insert(objects[i]);
        }
    //some getters, setters, insert, search and etc...
}

我也有MyQueue Class:

public class MyQueue<E> {
    private LinkedList<E> list = new LinkedList<E>();

    public void enqueue(E e) {
        list.addLast(e);
    }

    public E dequeue() {
        return list.removeFirst();
    }

    public int getSize() {
        return list.size();
    }

    public String toString() {
        return "Queue: " + list.toString();
    }
}

最后我有了CreditCardTransaction类:

public class CreditCardTransaction {
    private int cardNumber;
    private String customerName;
    private int amount;

    public CreditCardTransaction(int cardNumber, String customerName, int amount) {
        this.cardNumber = cardNumber;
        this.customerName = customerName;
        this.amount = amount;
    }

    public int getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(int cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
}

在我写的主要内容中:

BinaryTree<MyQueue<CreditCardTransaction>> e = new BinaryTree<MyQueue<CreditCardTransaction>>();

我收到错误:

Bound mismatch: The type MyQueue<CreditCardTransaction> is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type BinaryTree<E>

我尝试各种类型的混合,只要日食给我这个错误,请帮忙吗?

修改

我添加到MyQueue

public class MyQueue<E> implements Comparable<E>{
@Override
    public int compareTo(E o) {
        // TODO Auto-generated method stub
        return 0;
    }
}

并且主要还是给了我错误:

Bound mismatch: The type MyQueue<CreditCardTransaction> is not a valid
substitute for the bounded parameter <E extends Comparable<E>> of the
type BinaryTree<E>

1 个答案:

答案 0 :(得分:2)

MyQueue未实现Comparable,因此无法替换BinaryTree的类型参数。

如果您不需要,可以从extends Comparable<E>移除BinaryTree,或者更改MyQueue以实施Comparable<MyQueue<E>>