気象追跡機能を備えた航空管制塔をシミュレートする演習に取り掛かりました。Java - オブジェクトに別のメソッドのパラメータセットを使用するように指示する方法
私はプライベートコンストラクタを持つ座標クラスを持っています。コンストラクタは、経度、緯度、高さの3つの引数をとります。 引数を取る航空機クラス。座標と名前を座標にします。航空機クラスは、ジェットプレーン、ヘリコプター、バルーンの3つのクラスに継承され、そのコンストラクターは航空機と同じ引数をとります。
私は、3つのオブジェクトのいずれかを作成するために、ファクトリクラスを使用する必要があります。私の問題は、ファクトリメソッドは名前、型、経度、緯度、高さを引数として取りますが、返されるオブジェクトはCoordinatesオブジェクトを要求します。
Coordinatesオブジェクトを作成するために、ファクトリクラスからパラメータを取得する必要があることをどのように伝えますか?私はmakeCoordinatesメソッドを試しましたが、静的にすべての座標が0になるように設定した場合、静的でなくてもCoordinatesオブジェクトを作成せずに呼び出す方法はありますか?
練習の一環として、パラメータとアクセス指定子を削除または追加したり、型を変更することはできません。したがって、Coordinatesコンストラクタは非公開のままでなければなりません。
(飛行可能レジスタ及び更新方法とのインタフェースである)
ここでは、座標クラスである
public class Coordinates {
private int longitude;
private int latitude;
private int height;
public int getLongitude() {
return longitude;
}
public void setLongitude(int longitude) {
this.longitude = longitude;
}
public int getLatitude() {
return latitude;
}
public void setLatitude(int latitude) {
this.latitude = latitude;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
private Coordinates(int latitude, int longitude, int height){
}
public static Coordinates makeCoordinate(int longitude, int latitude, int height) {
return new Coordinates(longitude, latitude, height);
}
}
ファクトリクラス
public class ConcreteAircraftFactory extends AircraftFactory {
public Flyable newAircraft (String type, String name, int longitude, int latitude, int height){
Coordinates coord = Coordinates.makeCoordinate(longitude, latitude, height);
if (type.equals("Baloon") || type.equals("baloon")) {
return new Baloon(name, coord);
}
else if(type.equals("JetPlane") || type.equals("jetplane") || type.equals("Jetplane")) {
return new JetPlane(name, coord);
}
else if(type.equals("Helicopter") || type.equals("helicopter")) {
return new Helicopter(name, coord);
}
else
return null;
}
}
航空機クラス
public class Aircraft {
protected long Id;
protected String name;
protected Coordinates coordinates;
private long idCounter;
public long getId() {
return Id;
}
public void setId(long id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Coordinates getCoordinates() {
return coordinates;
}
public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
}
public long getIdCounter() {
return idCounter;
}
public void setIdCounter(long idCounter) {
this.idCounter = idCounter;
}
public Aircraft(String name, Coordinates coordinates) {
this.name = name;
this.coordinates = coordinates;
}
private long nextId() {
Id = getIdCounter() +1;
idCounter++;
return Id;
}
}
や航空機
public class Baloon extends Aircraft implements Flyable {
private WeatherTower weatherTower;
private String text;
public Baloon(String name, Coordinates coordinates) {
super(name, coordinates);
}
public void updateConditions() {
String newWeather = weatherTower.getWeather(coordinates);
switch(newWeather) {
case WeatherType.FOG:
coordinates.setHeight(coordinates.getHeight()-3);
text ="Baloon #" + this.getName() + "(" + this.getId() + "): get us lower, we are flying through pea soup";
try(PrintWriter out = new PrintWriter("Simulation.txt")){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case WeatherType.RAIN:
coordinates.setHeight(coordinates.getHeight()-5);
text ="Baloon #" + this.getName() + "(" + this.getId() + "): descending will not make us any less wet";
try(PrintWriter out = new PrintWriter("Simulation.txt") ){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case WeatherType.SUN:
coordinates.setHeight(coordinates.getHeight()+4);
coordinates.setLongitude(coordinates.getLongitude()+2);
text ="Baloon #" + this.getName() + "(" + this.getId() + "): make twoards the rising sun";
try(PrintWriter out = new PrintWriter("Simulation.txt") ){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case WeatherType.SNOW:
coordinates.setHeight(coordinates.getHeight()-15);
text ="Baloon #" + this.getName() + "(" + this.getId() + "): this thing does not run a cold air";
try(PrintWriter out = new PrintWriter("Simulation.txt") ){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
}
if(coordinates.getHeight()<0) {
coordinates.setHeight(0);
}
if(coordinates.getHeight()>100) {
coordinates.setHeight(100);
}
if (coordinates.getHeight()==0) {
weatherTower.unregister(this);
String text ="Tower Says: Baloon #" + this.getName() + "(" + this.getId() + "): has been unrergistered";
try(PrintWriter out = new PrintWriter("Simulation.txt") ){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public void registerTower(WeatherTower weatherTower) {
weatherTower.register(this);
text ="Tower Says: Baloon #" + this.getName() + "(" + this.getId() + "): registered to weather tower";
try(PrintWriter out = new PrintWriter("Simulation.txt") ){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
ありがとうございました。これはうまくいった。 もう1つ質問があります。メインで作成したオブジェクトにアクセスするにはどうすればよいですか? 新しいファクトリオブジェクトを作成しました ConcreteAircraftFactoryファクトリ= new ConcreteAircraftFactory(); といい、newAircraftメソッドと呼ばれます。 factory.newAircraft(parameters ...); 今作成したオブジェクトのメソッドを呼び出すにはどうすればよいですか?私はそれをfactory.method()に与えることはできません。 ArrayListに入れますか? – Adi
一般的には、作成したオブジェクトを変数で参照します。 'Flyable myAirCraft = factory.newAircraft(...)'です。次に、その上の 'Flyable'インタフェースと互換性のあるメソッドを呼び出すことができます。例: 'myAirCraft.fly()' – davidxxx
ありがとうございました。私はまだあなたを+1することができません:( – Adi