對象我有代碼看起來像這樣:印刷只有一個從一個HashMap
public Flight{
String code;
char status;
char type;
Flight(String code, char status, char type){
this.code = code;
this.status = status;
this.type = type;
}
}
public Menu{
Flight flight1 = new Flight("DL123",'A','D');
Flight flight2 = new Flight("DL146",'A','I');
flightMap.put("DL123", flight1)
flightMap.put("DL146", flight2)
}
if(schedule.flightMap.containsKey(decision))
{
}
如果用戶輸入DL123和containsKey方法返回true,我想回到只 flight1的對象屬性。我將如何能夠做到這一點?我已經嘗試覆蓋toString,但因爲toString只能返回字符串,我不知道如何返回狀態和字符屬性。
請詢問您是否需要更多信息!
從map中調用並調用toString(),假設toString()被任何想要打印的內容覆蓋。 – kosa
如果您想要從單個函數返回所有屬性,則需要將它們包裝在單個對象中,因爲函數只能返回一件事......所以您只需返回整個對象。對象的字段(或屬性)不獨立於該對象存在。 – ApproachingDarknessFish
請詳細解釋你的問題。當您調用flightMap.get(「DL123」)時,您將在地圖中添加具有字符串鍵(例如「DL123」)的飛行物體,它將自動返回與該鍵相關的對象。你是否只想返回一個屬性或飛行對象。 –