简体   繁体   English

Java-如何告诉对象使用不同方法中的一组参数

[英]Java - how to tell an object to use a set of parameters from a different method

I'm working on an exercise which simulates an air traffic control tower with weather tracking features. 我正在做一个练习,该练习模拟具有天气跟踪功能的空中交通管制塔台。

I have a coordinates class which has a private constructor. 我有一个具有私有构造函数的坐标类。 The constructor takes 3 arguments, longitude, latitude and height. 构造函数采用3个参数,即经度,纬度和高度。 An aircraft class which takes with the arguments Coordinates coordinates and name. 一个带有“坐标”坐标和名称作为参数的飞机类。 The aircraft class is inherited by 3 classes JetPlane, Helicopter and Baloon whose constructors take the same arguments as Aircraft. 飞机类是由3类JetPlane,Helicopter和Baloon继承的,它们的构造函数采用与Aircraft相同的论点。

As part of the exercise I have to use a factory class to create any of the 3 objects. 作为练习的一部分,我必须使用工厂类来创建3个对象中的任何一个。 My problem is that the factory method takes as arguments name, type, longitude, latitude and height but the objects which it returns ask for a Coordinates object. 我的问题是,工厂方法将名称,类型,经度,纬度和高度作为参数,但是返回的对象要求一个Coordinates对象。

How can I tell it that it should take the parameters from the factory class to make the Coordinates object? 我怎么能告诉它应该使用工厂类的参数来制作Coordinates对象? I have tried with a makeCoordinates method but if I set it to static that all coordinates will be 0. Is there any way to call it without it being static and without having to create a Coordinates object? 我尝试使用makeCoordinates方法,但是如果将其设置为static,则所有坐标都将为0。是否有任何方法可以调用它而不使其成为静态对象,而不必创建Coordinates对象?

As part of the exercise I am not allowed to remove or add any parameters and access specifiers or change their type. 作为练习的一部分,我不允许删除或添加任何参数以及访问说明符或更改其类型。 So the Coordinates constructor will have to remain private. 因此,Coordinates构造函数必须保持私有状态。

(Flyable is an interface with a register and update method) (可移植是具有寄存器和更新方法的接口)

Here is the Coordinates class 这是座标课

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

} }

The factory class 工厂班

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;

}

}

The Aircraft class 飞机课

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

}

And one of the 3 classes which inherit Aircraft 是继承飞机的三大类之一

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

}
}

Actually the factory method of Coordinates invokes the Coordinates private constructor but it has an empty body. 实际上, Coordinates的factory方法调用了Coordinates私有构造函数,但它具有空主体。
So it doesn't value any field of Coordinates : 因此,它不重视任何Coordinates字段:

private Coordinates(int latitude, int longitude, int height){
}

Just set the fields of the currently created object with the passed parameters : 只需使用传递的参数设置当前创建的对象的字段:

private Coordinates(int latitude, int longitude, int height){
   this.latitude = latitude;
   this.longitude= longitude;
   this.height= height;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM