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 {

这意味着这些方法的接口必须具有相同的“抛出”。

顺便说一句,我会制作QueueFullExceptionQueueEmptyException扩展不是检查异常的IllegalStateException,但我仍会将其添加到界面中的 throws 子句中。

为了比较,您可以查看队列,我会尽可能地遵循它抛出的命名和异常。

我会考虑将您的 FixedBuffer 变成一个环形缓冲区,也称为循环缓冲区 这样,您的队列不会因为到达末尾而用完空间。


这就是我基于 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 As the documentation notes,offer更可取,add并且您可能希望根据您的要求选择其中之一。

于 2012-11-06T08:38:07.730 回答
2

在您的FixedQueue课程中,您的方法putthrows QueueFullException,但未在您的 interface 中指定ICharQ。与get和相同QueueEmptyException

您可以:

  • 在界面中也指定这些异常
  • 或使这两个例外都扩展RuntimeException而不是Exception
于 2012-11-06T08:38:09.280 回答