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

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

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

  1. public interface BinTreeGenInterface<E extends Comparable<E>> {
  2. /**
  3. * counts all nodes in the subtree of k (inclusive k)
  4. * @param k given node
  5. * @return number of nodes in the subtree of k
  6. */
  7. public abstract int countNodes(BinNodeGen<E> k);
  8. /**
  9. * counts all nodes in the tree
  10. * @return number of nodes
  11. */
  12. public abstract int countNodes();
  13. /**
  14. * inserts an item into a sorted subtree if the item does not already exist
  15. * and returns true, if the item was successfully inserted
  16. * @param item to be inserted
  17. * @return true, if item was successfully inserted
  18. */
  19. public abstract boolean insertNode(E item);
  20. /**
  21. * searches for an item in a sorted subtree
  22. * @param item to search for
  23. * @return node with the searches item
  24. */
  25. public abstract BinNodeGen<E> find(E item);
  26. /**
  27. * returns all nodes of the subtree of k as a String
  28. * @param k given node
  29. * @return String representation of the subtree of k
  30. */
  31. public abstract String toString(BinNodeGen<E> k);
  32. /**
  33. * returns all nodes of the tree as a String
  34. * @return String representation of the tree
  35. */
  36. public abstract String toString();
  37. }

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

  1. public class BinTreeGen<T> implements BinTreeGenInterface<E extends Comparable<E>>{
  2. /**
  3. *
  4. * Klasse zum erstellen eines Binaerknotens
  5. *
  6. */
  7. class BinNodeGen<B>{
  8. private B data;
  9. private B left, right;
  10. public B getData() {
  11. return data;
  12. }
  13. public void setData(B data) {
  14. this.data = data;
  15. }
  16. public B getLeft() {
  17. return left;
  18. }
  19. public void setLeft(B left) {
  20. this.left = left;
  21. }
  22. public B getRight() {
  23. return right;
  24. }
  25. public void setRight(B right) {
  26. this.right = right;
  27. }
  28. /**
  29. * Konstruktor BinNode
  30. * @param d übernimmt einen int Wert welcher den Inhalt eines Knoten zuschreiben soll
  31. */
  32. BinNodeGen(B d) {
  33. data = d;
  34. left = right = null;
  35. }
  36. /**
  37. * zusaetzlicher Konsruktor um Knoten direkt zu erzeugen
  38. * @param d uebernimmt einen int Wert welcher den Inhalt eines Knoten zuschreiben soll
  39. * @param l uebernimmt den Wert für einen Kindsknoten links
  40. * @param r uebernimmt den Wert für einen Kindsknoten rechts
  41. */
  42. BinNodeGen(B d,B l, B r) {
  43. data = d; left = l; right = r;
  44. }
  45. }
  46. private BinNodeGen<B> root = null;
  47. /**
  48. * Konstruktor für BinNode
  49. * @return
  50. */
  51. void BinTree() {
  52. root = null;
  53. }
  54. /**
  55. * zusaetzlicher Konsruktor um Binaerbaum direkt zu erzeugen
  56. * @param rn bekommt Binaeknoeten uebergeben aus denen ein Binaerbaum gebildet wird
  57. */
  58. BinTree(BinNode rn) {
  59. root = rn;
  60. }
  61. }

(我知道里面满是虫子)

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中使用它

相关问题