gpt4 book ai didi

Java - 如何告诉对象使用来自不同方法的一组参数

转载 作者:行者123 更新时间:2023-12-01 20:16:59 26 4
gpt4 key购买 nike

我正在开展一项模拟具有天气跟踪功能的空中交通管制塔的练习。

我有一个坐标类,它有一个私有(private)构造函数。构造函数有 3 个参数:经度、纬度和高度。一个飞机类,其参数为坐标坐标和名称。飞机类由 JetPlane、Helicopter 和 Baloon 3 个类继承,它们的构造函数采用与 Aircraft 相同的参数。

作为练习的一部分,我必须使用工厂类来创建 3 个对象中的任何一个。我的问题是,工厂方法将名称、类型、经度、纬度和高度作为参数,但它返回的对象需要一个坐标对象。

我如何告诉它应该从工厂类中获取参数来创建坐标对象?我尝试过使用 makeCooperatives 方法,但如果我将其设置为静态,则所有坐标都将为 0。有没有什么方法可以在不使其成为静态且无需创建坐标对象的情况下调用它?

作为练习的一部分,我不允许删除或添加任何参数和访问说明符或更改其类型。因此坐标构造函数必须保持私有(private)。

(Flyable是一个具有注册和更新方法的接口(interface))

这是坐标类

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;
}

}

以及继承 Aircraft 的 3 个类之一

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();
}

}
}

最佳答案

实际上,坐标的工厂方法调用了坐标私有(private)构造函数,但它有一个空的主体。
因此它不会评估任何坐标字段:

private Coordinates(int latitude, int longitude, int height){
}

只需使用传递的参数设置当前创建的对象的字段:

private Coordinates(int latitude, int longitude, int height){
this.latitude = latitude;
this.longitude= longitude;
this.height= height;
}

关于Java - 如何告诉对象使用来自不同方法的一组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com