实现带参数 E e 的方法时,如何解决关于超类型的错误?

How to fix the error about supertype when implementing a method with parameter E e?

我需要创建一个基于数组的堆栈来从接口获取方法。我想从界面实现 push(E e) 方法,但出现以下错误:

The method push(E) of type ArrayStack<E> must override or implement a supertype method
public interface Stack<E> extends BasicCollection {

   public E top() throws EmptyStackException;

   public void push(E e);

   public E pop() throws EmptyStackException;

}
@Override
   public void push(E e) {
       if(size == arrayCapacity) {
           array = Arrays.copyOf(array, array.length * 2);
       }
       array[size] = e;
       size += 1;
   }

我该如何解决这个问题?它具有与界面中相同的参数。怎么了?

public class ArrayStack 实现 Stack

这是我的 class 声明。这个有效:

public class ArrayStack 实现 Stack

刚刚添加到类声明的堆栈中。

当您编写 implements Stack 时,您使用的是原始类型。

What is a raw type and why shouldn't we use it?

因为你在Stack<E>中没有指定泛型类型E,所以使用的是基类型Object,就好像你写了implements Stack<Object>一样。这意味着 class 应该实现方法 push(Object) 而不是 push(E).

想必你的意思是

class ArrayStack<E> implements Stack<E>