覆盖超类的方法。模糊参考编译时错误

时间:2012-02-12 17:32:48

标签: java oop inheritance override

我编写了一个使用左对齐数组实现双端队列的类。 我还想编写一个类来使用“循环”方法实现双端队列。 我说过,因为它会分享一些方法,我会把它变成一个子类。 当我尝试首先覆盖方法插入时,我得到编译时错误。

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的新手,发现课堂设计和继承令人困惑。

3 个答案:

答案 0 :(得分:2)

问题是您从CircularArrayBasedDeque类中删除了泛型,而是使用:

public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque<EltType>

当前定义的方式,CircularArrayBasedDeque类中的EltType与ArrayBasedDeque类中的EltType 不同,这就是覆盖无法正常工作的原因

答案 1 :(得分:0)

确保在扩展基类时传递EltType参数:

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

答案 2 :(得分:0)

您需要执行以下操作以消除编译时错误:

替换

public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque

public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque<EltType>