我有枚举,用字符串定义和初始化,如下所示
public enum Code {
OK( "OK" ),
ERROR_FILE_NOT_FOUND ( "FILE_NOT_FOUND xxxxxxxxx" ),
ERROR_AUTHENTICATION_FAILURE("AUTHENTICATION_FAILURE xxxxxxx" ),
ERROR_AUTHORIZATION_FAILURE("AUTHORIZATION_FAILURE xxxxxxx" ),
ERROR_DUPLICATE_DEF("DUPLICATE_DEF xxxxxxx"),
ERROR("ERROR xxxxxx")
private final String errorText;
private Code(String error) {
errorText = error;
}
}
如果我有枚举值,ERROR_FILE_NOT_FOUND如何获得相应的字符串值“FILE_NOT_FOUNDxxxxxxxxx”?
谢谢,
答案 0 :(得分:5)
您发布的代码无法编译。你正在寻找这个:
public enum Code {
OK( "OK" ),
ERROR_FILE_NOT_FOUND ( "FILE_NOT_FOUND xxxxxxxxx" ),
ERROR_AUTHENTICATION_FAILURE("AUTHENTICATION_FAILURE xxxxxxx" ),
ERROR_AUTHORIZATION_FAILURE("AUTHORIZATION_FAILURE xxxxxxx" ),
ERROR_DUPLICATE_DEF("DUPLICATE_DEF xxxxxxx"),
ERROR("ERROR xxxxxx");
private final String text;
private Code(String text) {
this.text = text;
}
public String getText() {
return this.text;
}
}