简体   繁体   English

通过getter / setter将数据传递给其他类

[英]Passing data to other classes via getter/setter

I'm having issues passing data from a GUI to other classes. 我在将数据从GUI传递到其他类时遇到问题。 Everything is initialized in the GUI and then from there the data is passed to another class where additional changes may take place: (simplified and missing parts for compilation) 一切都在GUI中初始化,然后从那里将数据传递到另一个类,在该类中可能进行其他更改:(用于编译的简化部分和缺失部分)

class GUI extends JFrame {
final Configuration conf = new Configuration();

GUI() {
    initComponents();
}

private void initComponents() {

    //Setup default values
    conf.expectFires(false);
    conf.expectRain(false);
    conf.expectDisaster(true);

    JComboBox<String> seasonTypes = new JComboBox<>(new String[]{"Winter", "Spring", "Summer", "Fall"});

    seasonTypes.setSelectedIndex(0);
    conf.setSeason(seasonTypes.getItemAt(seasonTypes.getSelectedIndex()));

    //Action listener for dynamic changes to whatever is selected
    seasonTypes.addActionListener(e -> {
        String season = seasonTypes.getSelectedItem().toString();
        if (!season.equals(""))
            conf.setSeason(season);
    });

    pack();
    setLocationRelativeTo(getOwner());
    setVisible(true);
}
}

The data is supposed to be saved to my configuration class which is a getter/setter class. 数据应该保存到我的配置类(getter / setter类)中。 I'm unable to retrieve any data unless I set it within the same class: 我无法检索任何数据,除非我在同一类中进行了设置:

Main class: 主班:

public class Main {

public static void main(String[] args) {
    Configuration conf = new Configuration();

    //code for GUI not included but pretend the GUI is called here

    //data does not come out it's basically unset.
    System.out.println("Data from GUI: [season]"+ conf.getSeason()+ " Season expectations: " + conf.expectations());

    //yet this works just fine. 
    conf.setSeason("Summer");
    System.out.println("What's the last season set?: " + conf.getSeason()); 
}
}

Looks like you are creating two instances of your Configuration class. 看起来您正在创建Configuration类的两个实例。 One in the GUI and one in your Main class. 一个在GUI ,一个在您的Main类中。 These are not shared. 这些不是共享的。

If you want to use the Configuration from the GUI try adding a getter for your Configuration within the GUI class 如果要从GUI使用Configuration ,请尝试在GUI类中为Configuration添加吸气剂

public Configutation getConfig()
{
     return conf;
}

then in your main try: 然后在您的主要尝试中:

public class Main {

    public static void main(String[] args) {
    //code for GUI not included but pretend the GUI is called here
    // Assuming something like:
    GUI gui = new GUI();

    Configuration conf = gui.getConfig();

    System.out.println("Data from GUI: [season]"+ conf.getSeason()+ " Season expectations: " + conf.expectations());
    }
}

Another option would be to create your Configuration as a Singleton - then you get the same instance rather then instantaiting a new one each time. 另一种选择是将Configuration创建为单例-然后您获得相同的实例,而不是每次实例化一个新实例。 Here is an example of how to do this 这是如何执行此操作的示例

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

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