如何从 class 中 return 一个对象,当构造函数创建一个新的 HashMap 时,这个对象究竟是什么?

How to return an object from a class, and what exactly is this object when a constructor creates a new HashMap?

我一直在做一堆练习,这一切都说得通,但是当我突然不得不自己从头开始编写代码时,我很困惑,如果有人能帮我一点忙,那就是太好了。

我有:

public class Airplane {
  private HashMap<String,Integer> plane;
  public Airplane() {
    this.plane = new HashMap<String,Integer>();
  }
  public addAirplane(String id, int capacity) {
    this.plane.put(id,capacity);


 public class Flight {
   private HashMap<Airplane, List<String>> flight;
   public Flight() {
     this.flight = new HashMap<Airplane,List<String>>();
   public addFlight(Airplane airplane, String departurePort, String destinationPort) {
     this.flight.put(airplane, Arrays.asList(departurePort, destinationPort));
    }

然后我有另一个 class,我在其中创建 Plane 和 Flight 的实例,

private Airplane airplane = new Airplane();
private Flight flight = new Flight();

在大多数情况下它似乎工作正常,但我不知道我应该在此处检索和添加什么..

我从用户那里读取了飞机 ID 和两个机场代码,我想做这样的事情:

flight.addFlight( //here I have Airplane's name//, departureAirport, destinationAirport);

所以基本上有两个问题:1) 如何在只有名称的情况下检索 "Airplane" 对象。 2) 这个 "Airplane" 对象到底是什么?是哈希表吗?它是 HashMap 的一个实例吗?如果我尝试将 HashMap 而不是 Airplane 传递给方法 flight.addFlight(Airplane,String,String),会有区别吗?

好的,我将尝试解释您想要实现的目标。请检查这是否是您真正想要的。

假设您想为机场设计一个应用程序。现在让我们保持简单。您可以稍后针对自己的用例扩展它。

让我们开始设计您的应用程序。

每个 Airport 都有 Flights 并且它有在这些航班上运营的设施。因此,机场和航班及相关对象的简单 class 设计如下所示:

public class Airport {
    private List<Flight> allFlights;

    public List<Flight> getAllFlights() {
        return allFlights;
    }

    public void setAllFlights(List<Flight> allFlights) {
        this.allFlights = allFlights;
    }

    /**
     * Add flights
     * @param flight
     */
    public void addFlights(Flight flight){
        allFlights.add(flight);
    }

    /**
     * Delete flights
     * @param flight
     */
    public void deleteFlight(Flight flight){
        for(Flight eachFlight : allFlights){
            if(eachFlight.equals(flight)){
                allFlights.remove(eachFlight);
                break;
            }
        }
    }

    /**
     * Update specific flight info.
     * 
     * @param flightId
     * @param airplane
     * @param source
     * @param destination
     * @return
     */
    public boolean updateFlight(long flightId, Airplane airplane, String source, String destination){
        boolean isUpdated = false;
        for(Flight eachFlight : allFlights){
            if(eachFlight.getFlightId() == flightId){
                eachFlight.setAirplane(airplane);
                eachFlight.setSourceName(source);
                eachFlight.setDestinationName(destination);
                isUpdated = true;
                break;
            }
        }
        return isUpdated;
    }
}

每个 Flight 都有一个 Airplane 以及源和目标 飞行和飞机只是普通的旧 Java 对象 (POJO)

public class Flight {
    private long flightId;
    private Airplane airplane;
    private String sourceName;
    private String destinationName;

    public long getFlightId() {
        return flightId;
    }

    public void setFlightId(long flightId) {
        this.flightId = flightId;
    }

    public Airplane getAirplane() {
        return airplane;
    }

    public void setAirplane(Airplane airplane) {
        this.airplane = airplane;
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getDestinationName() {
        return destinationName;
    }

    public void setDestinationName(String destinationName) {
        this.destinationName = destinationName;
    }

    //Override equals method
}

public class Airplane {
    private long id;
    private String make;
    private String airlinesCompany;

    public Airplane(long id, String make, String airlinesCompany) {
        this.id = id;
        this.make = make;
        this.airlinesCompany = airlinesCompany;
    }

    public long getId() {
        return id;
    }

    public String getMake() {
        return make;
    }

    public String getAirlinesCompany() {
        return airlinesCompany;
    }
}

我希望这个设计能清除你的想法。