这是完整的代码,这个程序应该可以工作,因为我从这本书中复制了它:
Java:初学者指南: Herbert Schildt
FixedQueue类:
// A fixed-size queue class for characters that uses exceptions.
class FixedQueue implements ICharQ {
private char q[]; // this array holds the queue
private int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public FixedQueue(int size) {
q = new char[size]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void put(char ch) throws QueueFullException {
if(putloc==q.length)
throw new QueueFullException(q.length);
q[putloc++] = ch;
}
// Get a character from the queue.
public char get() throws QueueEmptyException {
if(getloc == putloc)
throw new QueueEmptyException();
return q[getloc++];
}
}
接口ICharQ:
// A character queue interface.
public interface ICharQ {
// Put a character into the queue.
void put (char ch) throws QueueFullException;
// Get a character from the queue.
char get() throws QueueEmptyException;
}
QExcDemo类:
// Demonstrate the queue exceptions.
class QExcDemo {
public static void main(String args[]) {
FixedQueue q = new FixedQueue(10);
char ch;
int i;
try {
// over-empty the queue
for(i=0; i<11; i++) {
System.out.print("Getting next char: ");
ch = q.get();
System.out.println(ch);
}
}
catch(QueueEmptyException exc) {
System.out.println(exc);
}
}
}
QueueEmptyException类:
// An exception for queue-empty errors.
public class QueueEmptyException extends Exception {
public String toString() {
return "\nQueue is empty.";
}
}
QueueFullException类:
// An exception for queue-full errors.
public class QueueFullException extends Exception {
int size;
QueueFullException(int s) { size = s; }
public String toString() {
return "\nQueue is full. Maximum size is " + size;
}
}
当我构建程序时,为什么会出现异常错误?
运行:线程"main“dec15_javatutorial.FixedQueue.get(FixedQueue.java:28)中的异常:不可编译的源代码-- dec15_javatutorial.FixedQueue中的get()不能在dec15_javatutorial.ICharQ重写方法中实现get(),不要抛出dec15_javatutorial.QueueEmptyException获取下一个字符: at dec15_javatutorial.QExcDemo.main(QExcDemo.java:33) at dec15_javatutorial.QExcDemo.main(QExcDemo.java:33) java.lang.RuntimeException: 1
谢谢
发布于 2014-12-17 07:45:00
我已经把你所有的类复制到一个项目中并运行它。它编译并运行。
产出:
Getting next char:
Queue is empty.
我的猜测是,要么您更改了FixedQueue中的某些内容,然后忘记了保存,要么您错误地导入了一些东西。
https://stackoverflow.com/questions/27520331
复制相似问题