2017-08-09 33 views
0

我正在进行一项模拟具有天气跟踪功能的空中交通管制塔的练习。Java - 如何告诉对象使用不同方法的一组参数

我有一个坐标类,它有一个私人的构造函数。构造函数需要3个参数,经度,纬度和高度。 一个飞机类,带有参数坐标坐标和名称。该机型由三类JetPlane,Helicopter和Baloon继承,其构造者与飞机有相同的论点。

作为练习的一部分,我必须使用工厂类来创建3个对象中的任何一个。我的问题是,工厂方法需要参数名称,类型,经度,纬度和高度,但它返回的对象要求一个坐标对象。

我怎么能告诉它,它应该从工厂类的参数,使坐标对象?我已经尝试过使用makeCoordinates方法,但是如果将其设置为static,所有坐标都将为0.是否有任何方法可以调用它,而不需要它是静态的,并且不必创建Coordinates对象?

作为练习的一部分,我不允许删除或添加任何参数和访问说明符或更改其类型。因此坐标构造函数将不得不保持私密性。

(飞散性是与寄存器和更新方法的界面)

这里是坐标类

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); 
} 

}

工厂类

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; 

} 

} 

飞机类

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; 
} 

} 

而3类,其继承飞机

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(); 
    } 

} 
} 

回答

3

的一个事实上的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; 
} 
+0

谢谢。这工作。 还有一个问题。我如何访问我在main中创建的对象? 我创建了一个新的工厂对象 ConcreteAircraftFactory factory = new ConcreteAircraftFactory(); 并称为newAircraft方法 factory.newAircraft(parameters ...); 如何在我刚创建的对象上调用方法?我不能只给它factory.method()。我把它放在ArrayList中吗? – Adi

+0

通常您可以使用变量引用创建的对象。 'Flyable myAirCraft = factory.newAircraft(...)'。然后你可以调用任何兼容'Flyable'接口的方法。例如:'myAirCraft.fly()' – davidxxx

+0

非常感谢。我还不能+1你:( – Adi

相关问题