我有看起来像这样的代码:

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只能以String形式返回,所以我不知道如何返回状态和类型为字符的属性。

请询问您是否需要更多信息!

最佳答案

getter类中定义Flight方法,然后:

 if(schedule.flightMap.containsKey(decision)){
   Fligth matchingFlight = schedule.flightMap.get(decision);
   String code = matchingFlight.getCode();
   char status = matchingFlight.getStatus();
   char type = matchingFlight.getType();
 }

10-02 02:25
查看更多