【发布时间】:2021-03-27 09:58:25
【问题描述】:
我尝试实现一个接口,并在我从 Comparable 扩展的接口的泛型中。出于某种原因,这会导致出现错误消息,但我不知道为什么。
public class BinTreeGen<T> implements BinTreeGenInterface<E extends Comparable<E>>{}
这是两个错误:这一行有多个标记 - 标记“>>”上的语法错误,{ 预计在此之后 令牌 - 令牌“扩展”的语法错误,预期
接口代码:
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;
}
}
(我知道它充满了错误)
【问题讨论】:
-
您想在
BinTreeGenInterface<...>部分指定什么?你想写BinTreeGenInterface<BinTreeGen<T>>吗?请edit您的问题包括您拥有的源代码并描述类应如何实现哪些接口。 -
一个很好的Java泛型起点,我认为你需要刷新一些想法baeldung.com/…。
-
我添加了一点代码,希望问题能被理解。@Progman
标签: java binary-tree nodes