簡體   English   中英

如何使用bankSim類中的Event類方法?

[英]How can I utilize my Event class methods in bankSim class?

當我編譯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 

問題似乎是您正在調用Event.setWhich(),它引用的是事件類型,而不是事件對象。 為了使用非靜態方法,您必須首先實例化該對象:

Event test = new Event();

然后在該實例化對象上調用該方法:

test.setWhichQ(1);

同樣,如果您想按上面的說明使用setWhichQ()方法,則必須將Event更改為靜態類,並將whichQ設置為靜態字段。 在這種情況下,可以使用對類本身的引用來調用Event.setWhichQ(),並且可以同樣地修改whichQ。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM