我正在学习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 QueueFullException2个错误
发布于 2012-11-06 16:38:08
在FixedQueue中,您有检查过的异常。
public void put(char ch)
throws QueueFullException {
public char get()
throws QueueEmptyException {这意味着这些方法的接口必须有相同的“抛出”。
顺便说一句,我会让QueueFullException和QueueEmptyException扩展IllegalStateException,这不是一个检查异常,但我仍然会将它添加到您接口的throws子句中。
为了进行比较,您可以查看Queue,我将尽可能多地跟踪它抛出的命名和异常。
我会考虑把你的FixedBuffer变成一个环形缓冲区,也被称为Circular Buffer,这样你的队列就不会因为到达末尾而耗尽空间。
这就是我如何通过基于队列来设置接口。
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-06 16:38:09
在FixedQueue类中,方法put抛出QueueFullException,但接口ICharQ中没有指定这一点。get和QueueEmptyException也是如此。
您可以执行以下任一操作:
RuntimeException而不是Exceptionhttps://stackoverflow.com/questions/13247231
复制相似问题