[英]Java How to pass the value of a parent class variable to an inheriting object
我正在做空中交通塔演習。 我有一個帶有兩個變量id和idCounter的Aircraft類。 該類由其他3個類繼承-3種飛機類型。 我使用工廠設計模式從這3種類型中的每一種創建對象,對象在創建時保存在ArrayList中。 每個對象都應該有一個唯一的ID,而我應該使用idCounter,以確定它。
我寫了以下方法
public long nextId() {
setIdCounter(getIdCounter()+1);
return idCounter;
}
public void setId(long id) {
Id = nextId();
}
問題是我似乎無法更新對象的ID,所有ID都停留在0。
我嘗試用以下方法調用set方法
arrayName.get(i).setId();
但看不到它,並要求我在繼承類實現的接口中創建setId()
方法。
我也嘗試過這樣做
public long nextId() {
setId(getIdCounter()+1);
return id;
}
並用
arrayName.get(i).nextId();
但是它將無法正常工作,因為nextId不是靜態的,如果我將其設置為靜態,則還必須將id設置為靜態。
如何稱呼這個主對象或告訴我的對象更新其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();
}
}
}
如果可以看到代碼,我可能會更有幫助,但是我認為,如果將此“ idCounter”設置為靜態變量,它將可以使用。
static long idCounter;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.