2014-10-29 36 views
0

當我編譯我的bankSim類時,出現錯誤:非靜態方法setWhichQ(int)不能從靜態上下文中引用。我能做些什麼來引用bankSim類中的Event類方法?就在這之下是processArrival,它是發生錯誤的地方。之後是我的整個活動課。如何在bankSim類中使用我的Event類方法?

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

這是什麼編程語言?你所包含的唯一標籤是靜態方法。 – furkle 2014-10-29 20:26:29

+0

@furkle爪哇,對不起剛剛添加它的回合 – 2014-10-29 20:46:40

+0

添加了下面的解釋。 – furkle 2014-10-29 20:50:34

回答

0

問題似乎是你調用Event.setWhich(),它引用Event類型而不是Event對象。爲了使用非靜態方法,你必須先實例化對象:

Event test = new Event(); 

然後調用方法實例化對象:

test.setWhichQ(1); 

同樣,如果你想用setWhichQ ()方法,您必須將Event更改爲靜態類,並將whichQ設置爲靜態字段。在這種情況下,可以通過引用類本身來調用Event.setWhichQ(),並且可以同樣修改whichQ。

相關問題