我正在进行空中交通塔运动。我有一个飞机类与两个变量id和idCounter。该班级由3个其他班级继承 - 3种类型的飞机。 我使用工厂设计模式从这三种类型中的每一种创建对象,创建时对象保存在ArrayList中。每个对象应该有一个唯一的ID,我应该使用idCounter来确定它。Java如何将父类变量的值传递给继承对象
我写了下面的方法
public long nextId() {
setIdCounter(getIdCounter()+1);
return idCounter;
}
public void setId(long id) {
Id = nextId();
}
的问题是,我似乎无法更新对象的ID,所有ID都停留在0
我已经打过电话设置方法与
arrayName.get(i).setId();
,但它不能看到它,并要求我创建由继承类实现的接口一个setId()
方法。
我也试着这样做
public long nextId() {
setId(getIdCounter()+1);
return id;
}
与
arrayName.get(i).nextId();
调用它,但它不会工作,因为nextId也不是一成不变的,如果我让静态的我有使id也是静态的。
我该如何调用这个main或者告诉我的对象更新他们的id?
飞机类代码
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;
}
public long nextId() {
setIdCounter(getIdCounter()+1);
return idCounter;
}
}
工厂类
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;
}
}
主要
ArrayList<Flyable> ar = new ArrayList<Flyable>();
for (int i = 1; i <FileReader.fileList.size(); i++) {
ar.add(factory.newAircraft(FileReader.fileList.get(i)[0], FileReader.fileList.get(i)[1], Integer.parseInt(FileReader.fileList.get(i)[2]),
Integer.parseInt(FileReader.fileList.get(i)[3]), Integer.parseInt(FileReader.fileList.get(i)[4])));
}
其中一个继承类(都具有相同的实现)
public class JetPlane extends Aircraft implements Flyable{
private WeatherTower weatherTower;
private String text;
public JetPlane(String name, Coordinates coordinates) {
super(name, coordinates);
}
public void updateConditions() {
weatherTower= new WeatherTower();
String newWeather = weatherTower.getWeather(coordinates);
switch(newWeather) {
case WeatherType.FOG:
coordinates.setLatitude(coordinates.getLatitude()+1);
text ="JetPlane #" + this.getName() + "(" + this.getId() + "): it's really foggy down there";
try(PrintWriter out = new PrintWriter(new FileOutputStream("Simulation.txt", true))){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case WeatherType.RAIN:
coordinates.setLatitude(coordinates.getLatitude()+5);
text ="JetFighter #" + this.getName() + "(" + this.getId() + "): it's raining hard here";
try(PrintWriter out = new PrintWriter(new FileOutputStream("Simulation.txt", true))){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case WeatherType.SUN:
coordinates.setHeight(coordinates.getHeight()+2);
coordinates.setLatitude(coordinates.getLatitude()+10);
text ="JetFighter #" + this.getName() + "(" + this.getId() + "): flying in the sun is so much fun";
try(PrintWriter out = new PrintWriter(new FileOutputStream("Simulation.txt", true))){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case WeatherType.SNOW:
coordinates.setHeight(coordinates.getHeight()-7);
text ="JetFighter #" + this.getName() + "(" + this.getId() + "): that thing about winter that guy from that tv show once said";
try(PrintWriter out = new PrintWriter(new FileOutputStream("Simulation.txt", true))){
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); //de vazut
text ="Tower says: JetPlane #" + this.getName() + "(" + this.getId() + "): has been unrergistered";
try(PrintWriter out = new PrintWriter(new FileOutputStream("Simulation.txt", true))){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public void registerTower(WeatherTower weatherTower) {
weatherTower.register(this);
text ="Tower says: JetPlane #" + this.getName() + "(" + this.getId() + "): has been rergistered";
try(PrintWriter out = new PrintWriter(new FileOutputStream("Simulation.txt", true))){
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
您的setId方法只需要一个参数。所以你不能用空参数来调用它。但是你实际上并没有以任何方式使用传递的长。当你对传入的id做任何事情时,你为什么将它定义为setId(long id)? –
你可以分享抽象类的代码和创建继承飞机的代码吗? – arkantos
请提供所有课程。如果没有这些问题,难以回答关于遗传问题的问题吗? – reporter