如何指定来自不同 class 的许多不同对象的数组列表中的哪个 class 对象来自

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

我创建了一个 for each 循环来遍历数组列表中的所有对象。这个数组列表中的对象是在不同的 类 中构造的,它们都是同一超类的子 类。如何在循环体内引用对象所属的子类?

我做了什么:

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

我知道这不是一个有效的解决方案,但我最好的猜测是以某种方式使用 instanceof 来检查循环所在的当前项目属于哪个子类。编辑:我想做的是尝试调用我在子类 Onetime

中编写的名为 occursOn 的方法

为了一个答案,就这样投吧。

((Onetime) item).occursOn()