在枚举中调用超级类型构造函数之前,无法参考const.getText



用默认构造函数调用constructor时给出错误,但是直接调用默认构造函数的getText((方法正常工作。

我的怀疑是它与构造函数的调用顺序有关。


public enum Const {
    VALUE1,
    VALUE2;
    private final String text;
    Const() {
        //this(getText()); --> gives error - Cannot reference Const.getText before super type constructor has been called in enum
        this.text = getText();
    }
    Const(final String text) {
        this.text = text;
    }
    private String getText() {
        return "x";
    }
}

如果要使用此((链构造函数。它应该是构造体主体的第一行。

Const() {
this(param1); 
//Rest of the code...
}

最新更新