1 public enum EvalResultTest { 2 3 // 默认是无参数构造函数,可以加(),也可以不加 4 True(), 5 False(), 6 UNKNOWN, 7 EXCEPTION(); 8 9 //public abstract String status(boolean runningNot); 10 11 private int a; 12 13 // 如果定义了这个有参数构造函数,那上述枚举值定义只能是带(),且传入参数 14 // private EvalResultTest(int a) { 15 // this.a = a; 16 // } 17 }
enum Color { RED, GREEN, BLUE; }
每个枚举都是通过 Class 在内部实现的,且所有的枚举值都是 public static final 的。
以上的枚举类 Color 转化在内部类实现:https://www.runoob.com/java/java-enum.html
1 class Color 2 { 3 public static final Color RED = new Color(); 4 public static final Color BLUE = new Color(); 5 public static final Color GREEN = new Color(); 6 }
public enum CurResult { /** * 命中 */ True() { // 内部类 @Override public String status(boolean runningNot) { return runningNot ? "0" : "1"; } @Override public String toString() { return "True"; } }, /** * 不命中 */ False() { @Override public String status(boolean runningNot) { return runningNot ? "1" : "0"; } @Override public String toString() { return "False"; } }, /** * 未知 * */ Unknown() { @Override public String status(boolean runningNot) { return "2"; } @Override public String toString() { return "Unknown"; } }, /** * 异常 */ Exception() { @Override public String status(boolean runningNot) { return "3"; } @Override public String toString() { return "Exception"; } }, /** * 超阀值 */ Overflow() { @Override public String status(boolean runningNot) { return "4"; } @Override public String toString() { return "Overflow"; } }, /** * 被降级 */ Downgrade() { @Override public String status(boolean runningNot) { return "5"; } @Override public String toString() { return "Downgrade"; } }, /** * 测试相关命中 */ Test_True() { @Override public String status(boolean runningNot) { return runningNot ? "0" : "1"; } @Override public String toString() { return "Test_True"; } }, MISSING() { @Override public String status(boolean runningNot) { return "6"; } @Override public String toString() { return "MISSING"; } }; public static CurResult valueOf(boolean status) { return status ? True : False; } public abstract String status(boolean runningNot); }