1

私はJavaを学んでいます。固定キューのクラスに例外ハンドラを追加することになっています。インターフェイスを変更する必要があるようですが、方法がわかりません。

コード:

//ICharQ.java
package qpack;

public interface ICharQ {
    void put(char ch);

    char get();

    void reset();
}

//QExcDemo.java
package qpack;

class QueueFullException extends Exception {
    int size;

    QueueFullException(int s) { size = s; }

    public String toString() {
        return "\nQueue is full. Max size is " + size;
    }
}

class QueueEmptyException extends Exception {
    public String toString() {
        return "\nQueue is empty.";
    }
}


//Excerpt from IQClasses.java
package qpack;

class FixedQueue implements ICharQ {
        private char q[];
        private int putloc, getloc;

        public FixedQueue(int size) {
                q = new char[size+1];
                putloc = getloc = 0;
        }

        public void put(char ch)
         throws QueueFullException {
                if (putloc == q.length-1)
                        throw new QueueFullException(q.length-1);


                putloc++;
                q[putloc] = ch;
        }

        public char get()
         throws QueueEmptyException {
                if (getloc == putloc)
                        throw new QueueEmptyException();

                getloc++;
                return q[getloc];
        }

        public void reset() {
                putloc = getloc = 0;
        }
}

コンパイラ出力...

qpack/IQClasses.java:22: error: get() in FixedQueue cannot implement get() in ICharQ
public char get() 
            ^
   overridden method does not throw QueueEmptyException
qpack/IQClasses.java:12: error: put(char) in FixedQueue cannot implement put(char) in ICharQ
public void put(char ch) 
            ^
   overridden method does not throw QueueFullException

2 エラー

4

2 に答える 2

3

FixedQueue には、チェックされた例外があります。

    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {

これは、これらのメソッドのインターフェイスに同じ「スロー」が必要であることを意味します。

ところで、チェック例外ではないIllegalStateExceptionQueueFullExceptionを作成してQueueEmptyException拡張しますが、それでもインターフェイスの throws 句に追加します。

比較のために、 Queueを見てください。私はそれがスローするネーミングと例外を可能な限り追跡します。

FixedBuffer をCircular Bufferとも呼ばれる Ring Buffer に変えることを検討します。 このようにして、キューが最後に達したからといってスペースが不足することはありません。


これは、Queue に基づいてインターフェイスを設定する方法です。

public interface CharQueue {

    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     *
     * @param ch the char to add
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(char ch) throws IllegalStateException;

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     *
     * @return <tt>true</tt> if the element was added to this queue, else
     *         <tt>false</tt>
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(char ch);

    /**
     * Retrieves and removes the head of this queue.  This method throws an exception if this
     * queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    char remove() throws NoSuchElementException;

    /**
     * Removes all of the elements from this collection.
     * The collection will be empty after this method returns.
     */
    void clear();
}

このようにして、コード内にない場合は精神的に置き換えることができますQueue<Character>CharQueue ドキュメントのメモにofferあるaddように、要件に応じてこれらのいずれかを選択することをお勧めします。

于 2012-11-06T08:38:07.730 に答える
2

あなたのFixedQueueクラスでは、あなたのメソッドputは をスローしますQueueFullExceptionが、それはあなたのインターフェースでは指定されていませんICharQgetとと同じQueueEmptyExceptionです。

次のいずれかを実行できます。

  • これらの例外もインターフェイスで指定します
  • または、これらの例外の両方を拡張するのRuntimeExceptionではなく拡張しますException
于 2012-11-06T08:38:09.280 に答える