2017-08-10 57 views
-1

我正在進行空中交通塔運動。我有一個飛機類與兩個變量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(); 
    } 
    } 

}

+0

您的setId方法只需要一個參數。所以你不能用空參數來調用它。但是你實際上並沒有以任何方式使用傳遞的長。當你對傳入的id做任何事情時,你爲什麼將它定義爲setId(long id)? –

+1

你可以分享抽象類的代碼和創建繼承飛機的代碼嗎? – arkantos

+2

請提供所有課程。如果沒有這些問題,難以回答關於遺傳問題的問題嗎? – reporter

回答

1

我可能會更有益的,如果我可以看到代碼,但我認爲,如果你做這個「idCounter」靜態變量它會工作。

static long idCounter; 
+0

的代碼更新。對於那個很抱歉。我不允許爲任何事情重新定義訪問修飾符。 – Adi

+0

使其成爲靜態的,它增加了構造的工作。謝謝。 – Adi

+0

不客氣! –