当我编译我的bankSim类时,我得到错误:非静态方法setWhichQ(int)不能从静态上下文引用。我该怎么做才能在bankSim类中引用我的Event类方法?就在这之下是processArrival,它是发生错误的地方。之后是我的整个活动课程。
public void processArrival(Arrival arrEvent, Arrival[] inputData, int inputDataIndex,
SortedList<Event> eventList, QueueDSVector<Arrival> teller1, QueueDSVector<Arrival> teller2) {
boolean atFront1 = teller1.isEmpty(); // am I the only one here?
boolean atFront2 = teller2.isEmpty();
if (atFront1) { // if no other customers, then immediately get served
Departure newDep = new Departure(arrEvent.getArrTime(), arrEvent);
// because this customer's next Event will be a departure
eventList.insert(newDep); // put the departure into eventList
} // end if
else if (atFront2) { // if no other customers, then immediately get served
Departure newDep = new Departure(arrEvent.getArrTime(), arrEvent);
// because this customer's next Event will be a departure
eventList.insert(newDep); // put the departure into eventList
} // end if
else if ( teller1.size()< teller2.size() ) { //queue of teller 1 is less than teller 2
teller1.enqueue(arrEvent); // put new customer into bank line to wait
Event.setWhichQ(1);
}
else if (teller2.size() < teller1.size()) {
teller2.enqueue(arrEvent);
Event.setWhichQ(2);
}
public abstract class Event implements Comparable<Event> {
protected int whichQ;
public Event() {
whichQ = 0;
}
public int compareTo (Event other) {
int thisTime = this.getTime();
int otherTime = other.getTime();
return (thisTime - otherTime);
}
public int getWhichQ() {
return whichQ;
}
//
public void setWhichQ(int q) {
if (q >= 2)
whichQ = 2;
else if (q<=1) // < 0 = 0
whichQ = 1;
else
whichQ = q;
}
public abstract int getTime();
public abstract String toString();
} // end Event class
答案 0 :(得分:0)
问题似乎是你正在调用Event.setWhich(),它引用了Event类型,而不是Event对象。要使用非静态方法,必须首先实例化对象:
Event test = new Event();
然后在该实例化对象上调用该方法:
test.setWhichQ(1);
同样,如果您想使用上面指出的setWhichQ()方法,则必须将Event更改为静态类,并将whichQ设为静态字段。在这种情况下,可以使用对类本身的引用来调用Event.setWhichQ(),并且同样可以修改哪个Q.