简体   繁体   English

JAva获取类变量:多个方法或一个带开关的方法

[英]JAva get Class variables: Multiple methods or one Method with switch

Is there a best practice regarding the two options below to get variables from, in this case, the MainScene class ? 在下面的两个选项中,是否存在从MainScene类中获取变量的最佳实践? I actually have many more variables to get so I would end up with many other methods or 2-3 switch methods. 实际上,我还有更多变量要获取,因此我最终会使用许多其他方法或2-3个切换方法。

class MainScene MainScene类

Option 1 - Multiple Methods 选项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;
}

Option 2 - One Method with Switch 选项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;
    }
}

For the option 1, you can use lombok, which helps generate all the getter functions. 对于选项1,可以使用lombok,它有助于生成所有getter函数。 For the option 2, you can use reflection. 对于选项2,可以使用反射。

If using Maven, including 如果使用Maven,则包括

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.14.8</version>
</dependency>

and here's the code: 这是代码:

import lombok.Data;
public @Data
class ReflectionTest {
    String str = "abc";

    String str2 = "ecd";

    public void get() {
        try {
            System.out.println(this.getClass().getDeclaredField("str").get(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class App {
    public static void main(String[] args) {
        ReflectionTest reflectionTest = new ReflectionTest();
        // get value by option 2
        reflectionTest.get();
        // get value by option 1
        System.out.println(reflectionTest.getStr2());
    }
}

Usually Option 1, and most (all?) IDE can auto-generate setters/getter methods. 通常,选项1和大多数(全部?)IDE可以自动生成设置器/获取器方法。

Also, if you let an IDE do all the work, easier and less chances of introduced bugs. 同样,如果让IDE完成所有工作,则引入错误的机会会更容易,也更少。

In general you should have one 'getter' method for each variable. 通常,每个变量应该有一个“ getter”方法。 However if you have a larger number of variables that represent different attributes of the same type then you might want to consider using an attribute enumeration: 但是,如果您有更多的变量代表相同类型的不同属性,那么您可能需要考虑使用属性枚举:

public enum Field {
    LOAD, DOWN, FLIGHT, AC, AIRPORT;
}

public MainScene {
    private final EnumMap<Field,TextField> fields = new EnumMap<>(Field.class);

    public TextField getField(Field field) {
        return fields.get(field);
    }
}

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

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