重写超类的方法。模糊引用编译时错误

问题描述:

我写了一个类来实现一个使用左对齐数组的双端队列。 我也想编写一个类来实现一个使用“循环”方法的双端队列。 我说过,因为它会分享一些我将它作为子类的方法。 当我尝试重写该方法插入第一我得到一个编译时错误。重写超类的方法。模糊引用编译时错误

CircularArrayBasedDeque.java:6: reference to insertFirst is ambiguous, both method insertFirst(EltType) in ArrayBasedDeque and method insertFirst(EltType) in CircularArrayBasedDeque match 
      dq.insertFirst(i); 
      ^
CircularArrayBasedDeque.java:21: method does not override or implement a method from a supertype 
    @Override 
    ^

ArrayBasedDeque.java

public class ArrayBasedDeque <EltType> 
       implements Deque <EltType> 
{ 
    public static void main(String[] args) 
    { 
     ArrayBasedDeque dq = new ArrayBasedDeque(); 
     for(int element = 0; element < 64; element ++) 
     { 
      dq.insertFirst(element); 
      System.out.println(dq); 
     } 
     /**for(int element = 0; element < 25; element ++) 
      { 
       dq.insertLast(element); 
       System.out.println(dq); 
      } 
      */ 
     for(int element = dq.size(); element > 0; element --) 
     { 
       dq.removeFirst();$ 
       System.out.println(dq);$ 
     } 
    } 
    private final int INITIAL_CAPACITY = 2; 
    private int capacity; 
    private EltType elements[]; 
    private int first; 
    private int last; 
    public ArrayBasedDeque() 
    { 
     capacity = INITIAL_CAPACITY; 
     elements = (EltType[]) (new Object[INITIAL_CAPACITY]); 
     first = -1; 
     last = 0; 
    } 
    /** 
     * Returns the size of the Deque by 
     * returning the index of the first element + 1. 
     * @return the deque size. 
     */ 
    public int size() 
    { 
     return first + 1; 
    } 
    /** 
     * Returns true if and only if the deque is empty. 
     * @return true/false indication of emptiness. 
     */ 
    public boolean isEmpty() 
    { 
     return (first + 1 == last); 
    } 
    /** 
     * Return the first element of the deque; 
     * illegal if the deque is empty. 
     * @return the front element. 
     */ 
    public EltType first() 
    { 
     if(isEmpty()) 
     { 
      System.err.println("You cannot remove an element from an empty double sided queue"); 
     } 
     return elements[first]; 
    } 
    /** 
     * Return the last element of the deque; 
     * illegal if the deque is empty. 
     * @return the last element. 
     */ 
    public EltType last() 
    { 
     if(isEmpty()) 
     { 
      System.err.println("You cannot remove an element from an empty double sided queue"); 
     } 
     return elements[last]; 
    } 
    /** 
     * Insert item at the front of the deque. 
     * @param element: the item to be added. 
     */ 
    public void insertFirst(EltType element) 
    { 
     if(isFull()) 
     { 
      expand(); 
     } 
     elements[first + 1] = element; 
     first++; 
    } 
    /** 
     * Insert item at the rear the deque. 
     * @param element: the item to be added. 
     */ 
    public void insertLast(EltType element) 
    { 
     if(isFull()) 
     { 
      expand(); 
     } 
     for(int index = first + 1; index > 0; index--) 
     { 
      elements[index] = elements[ index - 1 ]; 
     } 
     elements[0] = element; 
     first++; 
    } 
    /** 
     * Return and remove the front element of the deque; 
     * Illegal if deque is empty. 
     */ 
    public EltType removeFirst() 
    { 
     if(isEmpty()) 
     { 
      System.err.println("You cannot remove an element from an empty double sided queue"); 
     } 
     if(isQuarterFull()) 
     { 
      contract(); 
     } 
     return elements[first--]; 
    } 
    /** 
     * Return and remove the last element of the deque; 
     * illegal if the deque is empty. 
     */ 
    public EltType removeLast() 
    { 
     if(isEmpty()) 
     { 
      System.err.println("You cannot remove an element from an empty double sided queue"); 
     } 
     if(isQuarterFull()) 
     { 
      contract(); 
     } 
     EltType last = elements[0]; 
     for (int index = 0; index < first; index ++) 
     { 
      elements[index] = elements[ index + 1 ]; 
     } 

     return last; 
    } 
    /** 
     * Return true if and only if the queue is full. 
     * @return boolean representsion of weather the queue is full. 
     */ 
    public boolean isFull() 
    { 
     return (size() == capacity); 
    } 
    /** 
     * Return true if and only if the queue is quarter full 
     * @return boolean representation of weather the queue is quarter full. 
     */ 
    public boolean isQuarterFull() 
    { 
     return (size() == capacity/4); 
    } 
    /** 
     * Doubles the capacity of the array representing the queue 
     */ 
    public void expand() 
    { 
     EltType[] tmp; 
     tmp = (EltType[]) (new Object[this.size() * 2]); 
     for(int element = 0; element < capacity ; element++) 
     { 
      tmp[element] = elements[element]; 
     } 
     capacity *= 2; 
     elements = tmp; 
    } 
    /** 
     * Halves the capacity of the array representing the queue 
     */ 
    public void contract() 
    { 
     EltType[] tmp; 
     tmp = (EltType[]) (new Object[ capacity/2 ]); 
     for (int element = 0; element < size(); element++) 
     { 
      tmp[element] = elements[element]; 
     } 
     capacity /= 4; 
     elements = tmp; 
    } 
    /** 
     * Returns a string representation of the deque. 
     * @return a string representation of the deque. 
     */ 
    @Override 
    public String toString() 
    { 
     String string = "{ "; 
     for (int index = 0; index <= first; index ++) 
     { 
      string = string + elements[index] + " "; 
     } 
     string = string + "}"; 
     string = string + "\n"; 
     string = string + "Size " + size(); 
     string = string + "\n"; 
     string = string + "Capacity " + capacity; 
     string = string + "\n"; 
     return string; 
    } 
} 

CircularArrayBasedDeque.java

public class CircularArrayBasedDeque <EltType> 
          extends ArrayBasedDeque 
{ 
    public static void main(String[] args) 
    { 
     CircularArrayBasedDeque dq = new CircularArrayBasedDeque(); 
     for (int i = 0; i < 20; i++) 
     { 
      dq.insertFirst(i); 
      System.out.println(dq); 
     } 
    } 
    private final int INITIAL_CAPACITY = 20; 
    private int capacity; 
    private int first; 
    private int last; 
    private EltType[] elements; 
    public CircularArrayBasedDeque() 
    { 
     capacity = INITIAL_CAPACITY; 
     elements = (EltType[]) (new Object[INITIAL_CAPACITY]); 
     first = 0; 
     last = 0; 
    } 
    @Override 
    public void insertFirst(EltType element) 
    { 
    } 
    @Override 
    public boolean isEmpty() 
    { 
     return (first == last); 
    } 
    @Override 
    public int size() 
    { 
     return (capacity - first + last); 
    } 
} 

感谢您对您的帮助提前。 我是OOP的新手,发现类设计和继承混乱。

+0

我不知道有每一行的末尾'$'提高清晰度。我更习惯于在没有行尾标记的情况下阅读代码。 ;) – 2012-02-12 17:41:09

+0

对不起。未来我会在复制之前在vim中关闭它:)另外感谢您编辑我的帖子:)对不起,它是如此混乱:P这是我的第一个。 – 2012-02-12 17:56:45

的问题是,你从你的CircularArrayBasedDeque类下降仿制药,改用:

public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque<EltType> 

方式你有它当前定义的,你的CircularArrayBasedDeque类中的EltType不同的从你的ArrayBasedDeque类中的EltType,这就是为什么覆盖不能正常工作。

+0

非常感谢jtahlborn的快速回复:) – 2012-02-12 17:54:48

+0

+1为好回答 – 2012-02-12 17:59:33

确保您通过EltType参数当您扩展基类:

public class CircularArrayBasedDeque <EltType> 
extends ArrayBasedDeque <EltType> { 
//      ^^^^^^^^^ 
+0

非常感谢John的快速回复:) – 2012-02-12 17:55:37

你需要做以下摆脱编译时错误:

更换

public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque 

public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque<EltType> 
+0

非常感谢您的快速回复:) – 2012-02-12 17:55:17