使用getter和setter在各个类之间传递值

时间:2019-08-29 09:25:41

标签: java javafx

我正在尝试通过setter和getter将serval值从一个类传递到另一个类。 当我在一类中使用setter时,我不知道如何在另一类中正确使用getter来接收该数据。

这是我来自3个班级的台词。

  1. Controller类中有一个方法,其中iam使用设置器。

2。这是Weather类的一部分,具有所有设置方法和获取方法。

    public static void PreparingWeather(String s){

        //Declaring variables
        String data = null;
        org.json.JSONObject weatherData = null;
        weather = new Weather();
        //Reading data from JSONObject
        try {
            weatherData = new org.json.JSONObject(s);
            JSONArray arr = weatherData.getJSONArray("weather");
            for(int i = 0; i < arr.length(); i++){
                weather.setID(arr.getJSONObject(i).getInt("id"));
                weather.setMainWeather(arr.getJSONObject(i).getString("main"));
                weather.setDescriptionWeather(arr.getJSONObject(i).getString("description"));
                weather.setIconWeather(arr.getJSONObject(i).getString("icon"));
            }```

//Main weather parameters
private int ID;
private String mainWeather = "";
private String descriptionWeather = "";
private String iconWeather = "";


//Constructor
public Weather() {}


public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public String getMainWeather() {
    return mainWeather;
}

public void setMainWeather(String mainWeather) {
    this.mainWeather = mainWeather;
}

public String getDescriptionWeather() {
    return descriptionWeather;
}

public void setDescriptionWeather(String descriptionWeather) {
    this.descriptionWeather = descriptionWeather;
}

public String getIconWeather() {
    return iconWeather;
}```

在第三类MenuController中,我想对仅在第一类中设置的值使用吸气剂。我一直在获取空值,因为我在此类中创建了一个新的Weather Object,但是我不知道如何正确地创建它。 得到这些值后,我想使用此数据设置服务标签。

如果尚不清楚,我将发送更多代码或告诉更多细节。 预先感谢。

0 个答案:

没有答案
相关问题