2016-03-02 90 views
0

当我编译我的代码,我收到以下错误......覆盖方法错误

BSLinkedList.java:10: error: BSLinkedList is not abstract and does not override abstract method push(T) in BoundedStack 
public class BSLinkedList <T> implements BoundedStack<T>{ 
    ^
    where T is a type-variable: 
    T extends Object declared in class BSLinkedList 

我试图以确保我使用的一切都是泛型类型T.所有的问题我发现这个话题说人们还没有从界面实现一个方法,但是我已经实现了方法push以及我的界面中的所有其他方法!我感谢任何人可以提供的帮助或指导!谢谢!

import java.util.EmptyStackException; 
import java.util.*; 
/** 
* The class BSLinkedList implements the Stack ADT as described 
* in cpsc331Stack using a linked list 
* 
* @version 1.0 
*/ 
public class BSLinkedList <T> implements BoundedStack<T>{ 
     public class StackNode<T> { 
     private T value; 
     public StackNode<T> next; 
     private int capacity; 
     public StackNode<T> top; 
     private int size; 

     public StackNode(T x, StackNode<T> n){ 
      value = x; 
      next = n; 
     } 


     public void BSLinkedList(int capacity) { 
     assert capacity >= 0; 

     LinkedList<T> stack = new LinkedList<T>(); 
     size = 0; 
     top = (StackNode<T>)null; 
    } 

     public boolean isEmpty(){ 
     assert size >= 0; 
      if (size == 0){ 
       return true; 
      } 
      else{ 
       return false; 
      } 
     } 

     public boolean isFull(){ 
      if (size == capacity){ 
       return true; 
      } 
      else{ 
       return false; 
      } 
     } 

     public int capacity(){ 
      return capacity; 
     } 

     public int size(){ 
      return size; 
     } 

     public void push(T x) { 
      if (isFull()){ 
       throw new FullStackException(); 
      }  
      else{ 
       ++size; 
       top = new StackNode<T>(x, top); 
      } 
     } 

     public T top(){ 
      if(isEmpty()){ 
       throw new EmptyStackException(); 
      } 
      else{ 
       return top.value; 
      } 
     } 

     public T pop(){ 
      if (isEmpty()){ 
       throw new EmptyStackException(); 
      } 
      else{ 
       T e = top.value; 
       top = top.next; 
       --size; 
       return e; 
      } 
     } 

    } 
} 

和接口...

/** 
* The BoundedStack interface represents the Bounded Stack ADT as described in CPSC 331. 
* This interface extends the interface cpsc331Stack. 
* 
* @version 1.0 
*/ 
public interface BoundedStack<T> extends cpsc331Stack<T>{ 

    /** 
    * Returns the number of elements currently on the stack. 
    * 
    * @return the number of elements on the stack 
    */ 
    public int size(); 

    /** 
    * Returns the maximum number of elements the stack can store. 
    * 
    * @return the maximum number of elements the stack can store 
    */ 
    public int capacity(); 

    /** 
    * Tests whether the stack is full. 
    * 
    * @return true if number of elements in the stack is equal to 
    * the stack's capacity, false otherwise 
    */ 
    public boolean isFull(); 

    /** 
    * Pushes the object x onto the top of the stack. 
    * 
    * @param x object to be pushed onto the stack. 
    * @throws FullStackException if the stack is full 
    */ 
    public void push (T x); 

} 

其他的实现,工作正常...

import java.util.EmptyStackException; 

/** 
* The class BSArray implements the Stack ADT as described 
* in cpsc331Stack using an array 
* 
* @version 1.0 
*/ 
public class BSArray <T> implements BoundedStack<T>{ 
     private T[] stack; 
     private int size; 
     private int top = -1;; 
     private int capacity; 

    /** 
    * Creates a new BSArray of size capacity 
    * 
    * @param capacity integer value of the maximum number of elements the array can store 
    */ 

     public BSArray(int capacity) { 
      assert capacity >= 0; 
      stack = (T[]) new Object[capacity]; 
     } 
    /** 
    * Tests whether or not the stack is empty. 
    * 
    * @return true if the stack is empty, false otherwise 
    */ 
     public boolean isEmpty(){ 
      if (size >= 0){ 
       return true; 
      } 
      else{ 
       return false; 
       } 
     } 
    /** 
    * Tests whether or not the stack is full. 
    * 
    * @return true if number of elements in the stack is equal to 
    * the stack's capacity, false otherwise 
    */ 
     public boolean isFull(){ 
      if (size == capacity){ 
       return true; 
      } 
      else{ 
       return false; 
      } 
     } 

    /** 
    * Returns the maximum number of elements the stack can store. 
    * 
    * @return the maximum number of elements the stack can store 
    */ 
     public int capacity(){ 
      return capacity; 
     } 

    /** 
    * Returns the number of elements currently on the stack. 
    * 
    * @return the number of elements on the stack 
    */ 
     public int size(){ 
      return size; 
     } 

    /** 
    * Pushes the object x onto the top of the stack. 
    * 
    * @param x object to be pushed onto the stack. 
    * @throws FullStackException if the stack is full 
    */ 
     public void push(T x){ 
      if (isFull()){ 
       throw new FullStackException(); 
      } 
      else{ 
       ++top; 
       ++size; 
       stack[top] = x; 
      } 
     } 
    /** 
    * Returns the object at the top of the stack. 
    * 
    * @return reference to the item at the top of the stack 
    * @throws EmptyStackException if the stack is empty 
    */ 
     public T top(){ 
      if(isEmpty()){ 
       throw new EmptyStackException(); 
      } 
      else{ 
       return (T) stack[top]; 
      } 
     } 
    /** 
    * Removes and returns the object at the top of the stack. 
    * 
    * @return reference to the item at the top of the stack 
    * @throws EmptyStackException if the stack is empty 
    */ 
     public T pop(){ 
      if (isEmpty()){ 
       throw new EmptyStackException(); 
      } 
      else{ 
       assert top >= -1; 
       T e = stack[top]; 
       stack[top] = null; 
       --top; 
       --size; 
       return e; 
      } 
     } 
} 

cpsc331Stack接口

/** 
* The cpsc331Stack interface represents the Stack ADT as described 
* in CPSC 331. 
* 
* @version 1.0 
*/ 
public interface cpsc331Stack<T> { 

    /** 
    * Tests whether the stack is empty. 
    * 
    * @return true if the stack is empty, false otherwise 
    */ 
    public boolean isEmpty(); 

    /** 
    * Pushes the object x onto the top of the stack. 
    * 
    * @param x object to be pushed onto the stack. 
    */ 
    public void push(T x); 

    /** 
    * Returns the object at the top of the stack. 
    * 
    * @return reference to the item at the top of the stack 
    * @throws EmptyStackException if the stack is empty 
    */ 
    public T top(); 

    /** 
    * Removes and returns the object at the top of the stack. 
    * 
    * @return reference to the item at the top of the stack 
    * @throws EmptyStackException if the stack is empty 
    */ 
    public T pop(); 
} 

回答

0

您已经在StackNode内部类中实现了方法,但它是实现该接口的外部类BSLinkedList。

另外,正如前面的回答中所提到的,push方法声明了一个不在接口方法签名中的异常。当您解决其他问题时,这将导致不同的错误。

**编辑你的第二个例子,你已经正确实现了实现接口的类中的方法(你没有内部类)。

这是你想要实现的吗?

/** 
* The class BSLinkedList implements the Stack ADT as described 
* in cpsc331Stack using a linked list 
* 
* @version 1.0 
*/ 
public class BSLinkedList<T> implements BoundedStack<T> { 
    private int capacity; 
    public StackNode<T> top; 
    private int size; 

    public void BSLinkedList(int capacity) { 
     assert capacity >= 0; 

     LinkedList<T> stack = new LinkedList<T>(); 
     size = 0; 
     top = null; 
    } 

    public boolean isEmpty() { 
     assert size >= 0; 
     if (size == 0) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public boolean isFull() { 
     if (size == capacity) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public int capacity() { 
     return capacity; 
    } 

    public int size() { 
     return size; 
    } 

    @Override 
    public void push(T x) { 
     if (isFull()) { 
      throw new FullStackException(); 
     } else { 
      ++size; 
      top = new StackNode<T>(x, top); 
     } 
    } 

    public T top() { 
     if (isEmpty()) { 
      throw new EmptyStackException(); 
     } else { 
      return top.value; 
     } 
    } 

    public T pop() { 
     if (isEmpty()) { 
      throw new EmptyStackException(); 
     } else { 
      T e = top.value; 
      top = top.next; 
      --size; 
      return e; 
     } 
    } 

    public class StackNode<T> { 
     private T value; 
     public StackNode<T> next; 

     public StackNode(T x, StackNode<T> n) { 
      value = x; 
      next = n; 
     } 

    } 

} 
+0

我如何更改第一个错误?我删除了前面提到的其他错误 – AndieM

+0

我刚刚编辑了我的答案,包括一个建议,但我不知道你不知道你在做什么。特别是我不确定你想用StackNode类做什么。 –

+0

是的,这比我在做的事更有意义。谢谢 – AndieM

1

这是一个有点投机,但我相信这个问题你看到的是因为你的push()方法实际上并没有覆盖你在界面中指定的确切签名。你有这样的:

public void push(T x) throws FullStackException { } 

但界面具有这样的:

public void push (T x); 

快速修复,您可以更新扔FullStackException接口。

顺便说一句,你看到的错误是一个标准的Java 101错误,当你扩展一个抽象类而不重写该类中包含的所有抽象方法时,会发生这种错误。所以你的编译器认为有一个抽象方法push()那里你永远不会覆盖(即使你做了,至少在功能上)。

+0

我试图删除它,它没有奏效。我也有另一个我附带的实施工作正常。 – AndieM

+0

您更改了原始帖子的代码。为什么?你不应该从你发布的两个片段中得到不同的结果。你确定你给我们的错误信息是最新的和准确的吗? –

+0

是的,我再次尝试了几次,并得到相同的消息 – AndieM

0

您的方法签名应该与它重写的方法完全匹配。因此,要么从BSLinkedList类的push方法中移除throws Exception,要么在BoundedStack接口的push方法中添加throws异常。另外,你的类覆盖了BoundedStack接口的所有方法。但是,可能会有某些在cpsc331Stack中定义的方法,您必须在您的课程中实施这些方法。 请张贴cpsc331Stack接口的源代码在这里,以便我们能够帮助您更好地

0

您实际上已经定义的方法大小(),该内部容量(),isFull(),推()内部类“StackNode”这是在“BSLinkedList”中定义的。如果这就是你实际想要的,那么“StackNode”类应该实现“BoundedStack”而不是实现“BoundStack”的“BSLinkedList”,否则该方法应该在“BSLinkedList”中定义。

`class BSLinkedList <T>{ 
     public class StackNode<T> implements BoundedStack<T>{ 
     private T value; 
     public StackNode<T> next; 
     private int capacity; 
     public StackNode<T> top; 
     private int size; 

     public StackNode(T x, StackNode<T> n){ 
      value = x; 
      next = n; 
     } 


     public void BSLinkedList(int capacity) { 
     assert capacity >= 0; 

     LinkedList<T> stack = new LinkedList<T>(); 
     size = 0; 
     top = (StackNode<T>)null; 
    } 

     public boolean isEmpty(){ 
     assert size >= 0; 
      if (size == 0){ 
       return true; 
      } 
      else{ 
       return false; 
      } 
     } 
     @Override 
     public boolean isFull(){ 
      if (size == capacity){ 
       return true; 
      } 
      else{ 
       return false; 
      } 
     } 
     @Override 
     public int capacity(){ 
      return capacity; 
     } 
     @Override 
     public int size(){ 
      return size; 
     } 
     @Override 
     public void push(T x) { 
      if (isFull()){ 
       throw new FullStackException(); 
      }  
      else{ 
       ++size; 
       top = new StackNode<T>(x, top); 
      } 
     } 

     public T top(){ 
      if(isEmpty()){ 
       throw new EmptyStackException(); 
      } 
      else{ 
       return top.value; 
      } 
     } 

     public T pop(){ 
      if (isEmpty()){ 
       throw new EmptyStackException(); 
      } 
      else{ 
       T e = top.value; 
       top = top.next; 
       --size; 
       return e; 
      } 
     } 

    } 
}`