【发布时间】:2012-12-21 04:02:51
【问题描述】:
我的代码如下所示:
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,我想返回 only flight1 的对象属性。我怎么能做到这一点?我尝试过覆盖 toString,但是因为 toString 只能作为字符串返回,所以我不知道如何返回作为字符的状态和类型属性。
如果您需要更多信息,请询问!
【问题讨论】:
-
假设 toString() 被您想要打印的任何内容覆盖,请从地图中获取并调用 toString()。
-
如果您希望从单个函数返回所有属性,则需要将它们包装在单个对象中,因为函数只能返回一件事......所以你只是返回反正整个对象。对象的字段(或属性)并不独立于该对象而存在。
-
请详细解释您的问题。当您调用 flightMap.get("DL123") 时,您正在使用字符串键在地图中添加飞行对象,例如“DL123”,它将自动返回与该键关联的对象。您是否只想返回一个属性或飞行对象。
标签: java object printing hashmap