Java如何将父类变量的值传递给继承对象

时间:2017-08-10 08:30:42

标签: java

我正在进行空中交通塔运动。我有一个带有两个变量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();
    }
    }

}

1 个答案:

答案 0 :(得分:1)

如果我能看到代码,我会更有帮助,但我认为如果你把这个'idCounter'变成一个静态变量就行了。

static long idCounter;