extends命令的错误消息,但我不知道为什么

zvms9eto  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(308)

我尝试实现一个接口,在接口的泛型中,我从comparable扩展。出于某种原因,这会导致出现错误消息,但我不知道原因。 public class BinTreeGen<T> implements BinTreeGenInterface<E extends Comparable<E>>{} 这是两个错误:此行有多个标记-标记“>>”上的语法错误,{此标记后应为-标记“extends”上的语法错误,应为
接口中的代码:

public interface BinTreeGenInterface<E extends Comparable<E>> {
    /**
     * counts all nodes in the subtree of k (inclusive k)
     * @param k given node
     * @return number of nodes in the subtree of k
     */
    public abstract int countNodes(BinNodeGen<E> k);

    /**
     * counts all nodes in the tree
     * @return number of nodes
     */
    public abstract int countNodes();

    /**
     * inserts an item into a sorted subtree if the item does not already exist 
     * and returns true, if the item was successfully inserted
     * @param item to be inserted
     * @return true, if item was successfully inserted
     */
    public abstract boolean insertNode(E item);

    /**
     * searches for an item in a sorted subtree
     * @param item to search for 
     * @return node with the searches item
     */
    public abstract BinNodeGen<E> find(E item);

    /**
     * returns all nodes of the subtree of k as a String
     * @param k given node
     * @return String representation of the subtree of k
     */
    public abstract String toString(BinNodeGen<E> k);

    /**
     * returns all nodes of the tree as a String
     * @return String representation of the tree
     */
    public abstract String toString();

}

我试图在bintreegen类中实现以下代码:

public class BinTreeGen<T> implements BinTreeGenInterface<E extends Comparable<E>>{

    /**
     * 
     * Klasse zum erstellen eines Binaerknotens
     *
     */
    class BinNodeGen<B>{
        private B data;
        private B left, right;

        public B getData() {
            return data;
        }

        public void setData(B data) {
            this.data = data;
        }

        public B getLeft() {
            return left;
        }

        public void setLeft(B left) {
            this.left = left;
        }

        public B getRight() {
            return right;
        }

        public void setRight(B right) {
            this.right = right;
        }

        /**
         * Konstruktor BinNode
         * @param d übernimmt einen int Wert welcher den Inhalt eines Knoten zuschreiben soll
         */
         BinNodeGen(B d) {
            data = d;
            left = right = null;
        }

        /**
         * zusaetzlicher Konsruktor um Knoten direkt zu erzeugen
         * @param d uebernimmt einen int Wert welcher den Inhalt eines Knoten zuschreiben soll
         * @param l uebernimmt den Wert für einen Kindsknoten links
         * @param r uebernimmt den Wert für einen Kindsknoten rechts
         */
        BinNodeGen(B d,B l, B r) {
            data = d; left = l; right = r;
        }
    }

    private BinNodeGen<B> root = null;

    /**
     * Konstruktor für BinNode
     * @return 
     */
    void BinTree() {
        root = null;
    }

    /**
     * zusaetzlicher Konsruktor um Binaerbaum direkt zu erzeugen
     * @param rn bekommt Binaeknoeten uebergeben aus denen ein Binaerbaum gebildet wird
     */
    BinTree(BinNode rn) {
        root = rn;
    }

}

(我知道里面满是虫子)

hivapdat

hivapdat1#

首先你的类bintreegen的头是错误的,因此你得到错误“绑定不匹配”!!!您的类标题是: public class BinTreeGen<T> implements BinTreeGenInterface<E extends Comparable<E>> { ....} 您应该将其更改为: public class BinNodeGen<T extends Comparable<T>> implements BinTreeGenInterface<T> { ....} 类binnodegen中的泛型typ t是comparable的子类,您可以在接口bintreegeninterface中使用它

相关问题