0

我有以下代码:

public interface StackInterface<T> {
   public T pop();
   public void push(T n);
}


public class myStack<T> implements StackInterface<Node<T>> {
  Node<T> head;
  Node<T> next;
  Node<T> tail;
  public myStack(T t) {
        head = new Node<T>(t);
        head.next = null;
        tail=head;
  }

public myStack() {
    head = null;
    tail=head;
}

public Node<T> pop() {
    if(head==null) {
        return null;
    }
    Node<T> t= head;
    head=head.next;
    return t;
}

public void push(T n) {
    Node<T> t = head;
    head = new Node<T>(n);
    head.next = t;
}

}

此代码显示以下错误:

在类声明行;它说它没有实现方法 public void push(T n); 在 public void push(T n) 线上它说:

myStack 的 push 方法与 StackInterface 的 push 具有相同的擦除功能,但不会覆盖它。

方法原型是相同的;添加@Override 什么都不做。为什么会这样?

4

2 回答 2

1

您需要以这种方式实现,然后您的模板匹配。

public class myStack<T> implements StackInterface<T>
于 2013-10-28T03:55:01.120 回答
0

因为您正在实施StackInterface<Node<T>>,所以 push 方法需要是

public void push(Node<T> n) {
}
于 2013-10-28T03:42:29.663 回答