Java - 如何告诉对象使用来自不同方法的一组参数
Java - how to tell an object to use a set of parameters from a different method
我正在进行一项模拟具有天气跟踪功能的空中交通管制塔的练习。
我有一个坐标 class,它有一个私有构造函数。构造函数有 3 个参数,经度、纬度和高度。
一架飞机 class 带有参数 Coordinates 坐标和名称。 aircraft class 由 3 classes JetPlane、Helicopter 和 Baloon 继承,它们的构造函数采用与 Aircraft 相同的参数。
作为练习的一部分,我必须使用工厂 class 来创建 3 个对象中的任何一个。我的问题是工厂方法将名称、类型、经度、纬度和高度作为参数,但它 returns 要求坐标对象的对象。
我如何告诉它应该从工厂 class 获取参数来制作 Coordinates 对象?我已经尝试使用 makeCoordinates 方法,但如果我将它设置为静态,则所有坐标都将为 0。有什么方法可以调用它而不是静态的并且不必创建 Coordinates 对象?
作为练习的一部分,我不允许删除或添加任何参数和访问说明符或更改它们的类型。因此 Coordinates 构造函数必须保持私有。
(Flyable是一个有注册和更新方法的接口)
这里是坐标class
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);
}
}
工厂class
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;
}
}
飞机class
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 个 classes 之一
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();
}
}
}
实际上 Coordinates
的工厂方法调用了 Coordinates
私有构造函数,但它有一个空体。
所以它不重视 Coordinates
的任何字段:
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;
}
我正在进行一项模拟具有天气跟踪功能的空中交通管制塔的练习。
我有一个坐标 class,它有一个私有构造函数。构造函数有 3 个参数,经度、纬度和高度。 一架飞机 class 带有参数 Coordinates 坐标和名称。 aircraft class 由 3 classes JetPlane、Helicopter 和 Baloon 继承,它们的构造函数采用与 Aircraft 相同的参数。
作为练习的一部分,我必须使用工厂 class 来创建 3 个对象中的任何一个。我的问题是工厂方法将名称、类型、经度、纬度和高度作为参数,但它 returns 要求坐标对象的对象。
我如何告诉它应该从工厂 class 获取参数来制作 Coordinates 对象?我已经尝试使用 makeCoordinates 方法,但如果我将它设置为静态,则所有坐标都将为 0。有什么方法可以调用它而不是静态的并且不必创建 Coordinates 对象?
作为练习的一部分,我不允许删除或添加任何参数和访问说明符或更改它们的类型。因此 Coordinates 构造函数必须保持私有。
(Flyable是一个有注册和更新方法的接口)
这里是坐标class
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);
}
}
工厂class
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;
}
}
飞机class
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 个 classes 之一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();
}
}
}
实际上 Coordinates
的工厂方法调用了 Coordinates
私有构造函数,但它有一个空体。
所以它不重视 Coordinates
的任何字段:
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;
}