猿问

如何为每个对象创建一个对象集合

例如,如何制作每个不同机场的飞机列表?


我想在这个例子中创建机场,当它是这个特定的对象(机场)时,我想添加一架飞机到这个机场的集合中。


如何制作例如每个不同机场的飞机列表?我想在这个例子中创建机场,当它是这个特定的对象(机场)时,我想添加一架飞机到这个机场的集合中。


例如:


public class Airport {

    private Plane plane;

    Queue<Plane> queueOfPlanes = new ArrayDeque<Plane>();


    public Airport(Plane plane) {

        this.plane = plane;

        queueOfPlanes.add(plane);

    }

我正在创建一个机场,当我拥有这个特定机场时,我想在这个机场的队列中收集飞机。


POPMUISE
浏览 145回答 2
2回答

牛魔王的故事

您首先为您的机场拥有一个不同的界面。喜欢:private Plane plane; ...public Airport(Plane plane) {那已经是错误的了。机场不需要特定的单架飞机才能成为机场。而是去:class Airport {&nbsp; private final List<Plane> currentPlanes = new ArrayList<>();&nbsp; private final String name;&nbsp; public Airport(String name) {&nbsp;&nbsp; &nbsp; this.name = name;&nbsp; }&nbsp; public void addPlane(Plane plane) { currentPlanes.add(plane); }&nbsp; public void removePlane(Plane plane) { currentPlanes.remove(plane); }这里的想法是:机场具有不会改变的特定属性(例如它的名称、位置等)。但是飞机来来去去。因此,您的机场对象需要一种方法来存储当前与其关联的飞机。

慕田峪9158850

有很多方法可以做到,但我认为HashMaps最适合您的场景,让我们看一个例子。HashMap<String, ArrayList<Plane>> mAirPorts = new HashMap<String, ArrayList<Plane>>();现在您需要创建对象平面public class Plane&nbsp;{&nbsp; &nbsp; private double maxWeight;&nbsp; &nbsp; private double emptyWeight;&nbsp; &nbsp; private double loadWeight;&nbsp; &nbsp; private double travelSpeed;&nbsp; &nbsp; private double flyHours;&nbsp; &nbsp; private double consumption;&nbsp; &nbsp; private double maxFuel;&nbsp; &nbsp; private double kerosinStorage;&nbsp; &nbsp; public Plane( double maxWeight, double emptyWeight, double loadWeight,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double travelSpeed, double flyHours, double consumption,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double maxFuel, double kerosinStorage )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.maxWeight&nbsp; &nbsp; &nbsp; = maxWeight;&nbsp; &nbsp; &nbsp; &nbsp; this.emptyWeight&nbsp; &nbsp; = emptyWeight;&nbsp; &nbsp; &nbsp; &nbsp; this.loadWeight&nbsp; &nbsp; &nbsp;= loadWeight;&nbsp; &nbsp; &nbsp; &nbsp; this.travelSpeed&nbsp; &nbsp; = travelSpeed;&nbsp; &nbsp; &nbsp; &nbsp; this.flyHours&nbsp; &nbsp; &nbsp; &nbsp;= flyHours;&nbsp; &nbsp; &nbsp; &nbsp; this.consumption&nbsp; &nbsp; = consumption;&nbsp; &nbsp; &nbsp; &nbsp; this.maxFuel&nbsp; &nbsp; &nbsp; &nbsp; = maxFuel;&nbsp; &nbsp; &nbsp; &nbsp; this.kerosinStorage = kerosinStorage < this.maxFuel&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? kerosinStorage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : this.maxFuel;&nbsp; &nbsp; }&nbsp; &nbsp; public double getMaxWeight()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return maxWeight;&nbsp; &nbsp; }&nbsp; &nbsp; public double getEmptyWeight()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return emptyWeight;&nbsp; &nbsp; }&nbsp; &nbsp; public double getLoadWeight()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return loadWeight;&nbsp; &nbsp; }&nbsp; &nbsp; public double getTravelSpeed()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return travelSpeed;&nbsp; &nbsp; }&nbsp; &nbsp; public double getFlyHours()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return flyHours;&nbsp; &nbsp; }&nbsp; &nbsp; public double getConsumption()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return consumption;&nbsp; &nbsp; }&nbsp; &nbsp; public double getMaxFuel()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return maxFuel;&nbsp; &nbsp; }&nbsp; &nbsp; public double getKerosinStorage()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return kerosinStorage;&nbsp; &nbsp; }&nbsp; &nbsp; public void setMaxWeight(double maxWeight)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.maxWeight = maxWeight;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEmptyWeight(double emptyWeight)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.emptyWeight = emptyWeight;&nbsp; &nbsp; }&nbsp; &nbsp; public void setLoadWeight(double loadWeight)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.loadWeight = loadWeight;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTravelSpeed(double travelSpeed)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.travelSpeed = travelSpeed;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFlyHours(double flyHours)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.flyHours = flyHours;&nbsp; &nbsp; }&nbsp; &nbsp; public void setConsumption(double consumption)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.consumption = consumption;&nbsp; &nbsp; }&nbsp; &nbsp; public void setMaxFuel(double maxFuel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.maxFuel = maxFuel;&nbsp; &nbsp; }&nbsp; &nbsp; public void setKerosinStorage(double kerosinStorage)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.kerosinStorage = this.kerosinStorage + kerosinStorage > maxFuel&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? maxFuel : this.kerosinStorage + kerosinStorage;&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; Returns the total weight of the plane. Which is: emptyWeight +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; weight of load + weight of kerosin.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Expect 1 liter Kerosin as 0.8 kg.&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; */&nbsp; &nbsp; public double getTotalWeight ()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return emptyWeight + loadWeight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + (kerosinStorage * 0.8);&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; How far can the plane fly with the current kerosin storage?&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; */&nbsp; &nbsp; public double getMaxReach ()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return (kerosinStorage / consumption) * travelSpeed;&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; Prevent flying further then possible (with the current kerosin) !&nbsp; &nbsp; */&nbsp; &nbsp; public boolean fly (double km)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (km <= 0 || getMaxReach() < km || getTotalWeight() > maxWeight)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; flyHours += (km / travelSpeed);&nbsp; &nbsp; &nbsp; &nbsp; kerosinStorage -= (km / travelSpeed) * consumption;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; ! The parameter 'liter' can be a negative number.&nbsp; &nbsp; &nbsp; &nbsp; Doesn't have to be overfilled.&nbsp; &nbsp; &nbsp; &nbsp; Prevent a negative number as value of the 'kerosinStorage' property !&nbsp; &nbsp; */&nbsp; &nbsp; public void fillUp (double liter)&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ((kerosinStorage + liter) > maxFuel)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kerosinStorage = maxFuel;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if ((kerosinStorage + liter) < 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kerosinStorage = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kerosinStorage += liter;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; Prevent illogical value-assignments !&nbsp; &nbsp; */&nbsp; &nbsp; public boolean load (double kg)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ((loadWeight + emptyWeight + kg) > maxWeight)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if ((emptyWeight + kg) < 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadWeight = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadWeight += kg;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Display flying hours, kerosin storage & total weight on t. terminal.&nbsp; &nbsp; public void info ()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Flying hours: " + flyHours + ", Kerosin: "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + kerosinStorage + ", Weight: " + getTotalWeight());&nbsp; &nbsp; }}现在只需将对象添加到您的 HashMap 中,例如:mAirPorts.put("airport_key", ArrayListContainingPlanes);您现在可以通过您的机场钥匙获取飞机,例如:ArrayList<Plane> mPlanes = mAirPorts.get("airport_key");if (mPlanes != null) {&nbsp; &nbsp; ...} else {&nbsp; &nbsp; //No such airport}
随时随地看视频慕课网APP

相关分类

Java
我要回答