![](/img/trans.png)
[英]How to get generic? class from json response? If one object can contain many different classes
[英]How can I specify which class an object in an array list of many different objects from different classes comes from
我為每個循環創建了一個遍歷數組列表中所有對象的對象。 該數組列表中的對象是在不同的類中構造的,這些類都是同一超類的子類。 如何在循環體內引用對象所屬的子類?
我所做的:
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中編寫的一個方法叫做
為了得到答案,將其像這樣投射。
((Onetime) item).occursOn()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.