枚举无法创建

时间:2015-11-17 12:38:07

标签: java enums enumeration

我正在尝试创建以下枚举,但Eclipse会出现以下错误:

  

语法错误,插入&#34 ;;"完成ClassBodyDeclarations

代码:

public class SE {

    private enum Separator {
        COMMA(","),
        SEMICOLON(";"),
        COLON(":");

        private String separator;
    }

}

更新

//still receiving the same error

public class ParameterParser {

private enum Separator {

    COMMA(","),
    SEMICOLON(";"),
    COLON(":");

    private String mSeparator;

    private Separator(String separator) {
        // TODO Auto-generated method stub
        this.mSeparator = separator;
    }

    public String getSeparator() {
        return this.mSeparator;
    }
}
}

2 个答案:

答案 0 :(得分:3)

添加构造函数

private Separator(String separator) {
    this.separator = separator;
}

答案 1 :(得分:2)

你缺少枚举的构造函数!

private Separator (String s) {
    separator = s;
}

<强>更新

public class ParameterParser {

    private enum Separator {

        COMMA(","),
        SEMICOLON(";"),
        COLON(":");

        private String mSeparator;

        private Separator(String separator) {
            this.mSeparator = separator;
        }

        public String getSeparator() {
            return this.mSeparator;
        }
    }

    public static void main(String[] args) {
        Separator comma = Separator.COMMA;
        Separator semic = Separator.SEMICOLON;
        Separator colon = Separator.COLON;

        System.out.println(comma + ": " + comma.getSeparator());
        System.out.println(semic + ": " + semic.getSeparator());
        System.out.println(colon + ": " + colon.getSeparator());
    }
}

此代码编译精细并输出:

COMMA: ,
SEMICOLON: ;
COLON: :

因此,如果您仍然遇到此代码的问题,那么它必须是其他内容!您必须向我们提供更多信息(例如,已经定义了Separator,在ParameterParser类中有更多代码等)。