1. 可以为枚举类添加私有字段,以及私有构造函数
1 enum MyEnum{ 2 ONE("one"),TWO("two"),THREE("three"); 3 4 private String msg; 5 6 private MyEnum(String msg){ 7 this.msg = msg; 8 } 9 public String getMsg(){ 10 return msg; 11 } 12 }
2. 枚举类中不同的元素可以对方法进行不同的实现或者覆写,类似于元素继承枚举类。
1 enum MyEnum{ 2 ONE{ 3 void action(){ 4 System.out.println("one do"); 5 } 6 }, 7 TWO{ 8 void action(){ 9 System.out.println("two do"); 10 } 11 }, 12 THREE{ 13 void action(){ 14 System.out.println("three do"); 15 } 16 }; 17 18 19 abstract void action(); 20 }
原文:https://www.cnblogs.com/ldlblog/p/14940021.html