通过 getter/setter 将数据传递给其他 类
Passing data to other classes via getter/setter
我在将数据从 GUI 传递到其他 classes 时遇到问题。一切都在 GUI 中初始化,然后数据从那里传递到另一个 class,可能会发生其他更改:(编译的简化和缺失部分)
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);
}
}
数据应该保存到我的配置 class,这是一个 getter/setter class。我无法检索任何数据,除非我将其设置在相同的 class:
主要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());
}
}
看起来您正在创建 Configuration
class 的两个实例。一个在 GUI
中,一个在你的 Main
class 中。这些不共享。
如果您想使用 GUI
中的 Configuration
,请尝试在 GUI
class 中为您的 Configuration
添加一个 getter
public Configutation getConfig()
{
return conf;
}
然后在你的主要尝试中:
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());
}
}
另一种选择是将 Configuration
创建为单例 - 然后您将获得相同的实例,而不是每次都实例化一个新实例。 Here is an example of how to do this
我在将数据从 GUI 传递到其他 classes 时遇到问题。一切都在 GUI 中初始化,然后数据从那里传递到另一个 class,可能会发生其他更改:(编译的简化和缺失部分)
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);
}
}
数据应该保存到我的配置 class,这是一个 getter/setter class。我无法检索任何数据,除非我将其设置在相同的 class:
主要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());
}
}
看起来您正在创建 Configuration
class 的两个实例。一个在 GUI
中,一个在你的 Main
class 中。这些不共享。
如果您想使用 GUI
中的 Configuration
,请尝试在 GUI
class 中为您的 Configuration
添加一个 getter
public Configutation getConfig()
{
return conf;
}
然后在你的主要尝试中:
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());
}
}
另一种选择是将 Configuration
创建为单例 - 然后您将获得相同的实例,而不是每次都实例化一个新实例。 Here is an example of how to do this