是否有关于下面两个选项获取变量,在这种情况下,MainScene类的最佳做法?实际上我有更多的变量可以得到,所以我最终会得到许多其他方法或者2-3个切换方法。JAva获取类变量:多种方法或一种方法与开关
类MainScene
选项1 - 多种方法
public TextField getLoadTxt(){
return loadTxt;
}
public TextField getdownTxt(){
return downTxt;
}
public TextField getflightTxt(){
return flightTxt;
}
public TextField getacTxt(){
return acTxt;
}
public TextField getairportTxt(){
return airportTxt;
}
选项2 - 一个具有开关
public TextField getTextField(String textField){
TextField text = new TextField();
text.setText("Default");
switch(textField){
case "loadTxt": return loadTxt;
case "downTxt": return downTxt;
case "flightTxt": return flightTxt;
case "acTxt": return acTxt;
case "airportTxt": return airportTxt;
default: return text;
}
}
通常选择1,和大多数(全部?)IDE可以自动生成setter方法/ getter方法。 –
你应该重新发布作为答案,我会选择它。 – neilnm