简体   繁体   中英

How can I specify which class an object in an array list of many different objects from different classes comes from

I've created a for each loop to go through all the objects in an array lists. The objects in this array list are constructed in different classes that are all subclasses of the same superclass. How can I refer to the subclass that the object belongs to within the body of the loop?

What I've done:

for (Appointment item: AppointmentBook.apps){


         if (item instanceof Onetime){
             //this isn't valid java but I don't know what to do here
             Onetime.item.occursOn(int month, int day, int year)
         }

    }//forloop

I know this isn't a valid solution, but my best guess would be to use instanceof in some way to check which subclass the current item the loop is on belongs to. Edit: What I'm trying to do is trying to call a method called occursOn that I've written in the subclass Onetime

为了得到答案,将其像这样投射。

((Onetime) item).occursOn()

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