简体   繁体   中英

How can I utilize my Event class methods in bankSim class?

when i compile my bankSim class, I get the error: non-static method setWhichQ(int) cannot be referenced from a static context. What can I do to reference my Event class methods in the bankSim class? Just below this is processArrival which is where the error occurs. After that is my entire event class.

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 

The problem appears to be that you're calling Event.setWhich(), which is referencing the Event type, not an Event object. In order to use a non-static method, you have to first instantiate the object:

Event test = new Event();

then call the method on that instantiated object:

test.setWhichQ(1);

Likewise, if you wanted to use the setWhichQ() method as you've indicated above, you'd have to change Event to a static class, and make whichQ a static field. In this case, Event.setWhichQ() could be called with a reference to the class itself, and could likewise modify whichQ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM