- /**
- * Effective Java 第二版
- * 第 30 条: 用 enum 代替 int 常量
- */
- import java.util.HashMap;
- import java.util.Map;
- public class EnumTest {
- /* 媒体操作 */
- public final static int START = 1;
- public final static int PAUSE = 2;
- public final static int RESUME = 3;
- public final static int STOP = 4;
- /* 返回结果 */
- public final static int RET_OK = 1;
- private static int playWithInt(int fun)
- {
- switch (fun)
- {
- case START:
- case PAUSE:
- case RESUME:
- case STOP:
- System.out.println("playWithInt do" + fun);
- break;
- default:
- }
- return RET_OK;
- }
- public enum PlayEm
- {
- START,PAUSE,RESUME,STOP // 命名空间使得他可以重名
- }
- private static void playWithEnum(PlayEm fun)
- {
- switch (fun)
- {
- case START:
- case PAUSE:
- case RESUME:
- case STOP:
- System.out.println("playWithEnum do" + fun);
- break;
- default:
- }
- }
- public enum SuperPlayEm
- {
- START(1,"START"),PAUSE(2,"PAUSE"),RESUME(3,"RESUME"),STOP(4,"STOP");
- private final int code;
- private final String name;
- SuperPlayEm(int code, String name) {
- this.code = code;
- this.name = name;
- }
- @Override
- public String toString() {
- return "SuperPlayEm{" +
- "code=" + code +
- ", name='" + name + '\'' +
- '}';
- }
- public void doPlay(String player)
- {
- switch (this)
- {
- case START:
- case PAUSE:
- case RESUME:
- case STOP:
- System.out.println("player :"+ player + "exec" + this.name());
- break;
- default:
- }
- }
- }
- private static void playWithSuperEnum(SuperPlayEm fun)
- {
- switch (fun)
- {
- case START:
- case PAUSE:
- case RESUME:
- case STOP:
- default:
- System.out.println("playWithSuperEnum do" + fun);
- }
- }
- public enum SuperPlayApplyEm
- {
- START(1,"START"){void doPlay(String player){System.out.println("player :"+ player + "exec START" );}},
- PAUSE(2,"PAUSE"){void doPlay(String player){System.out.println("player :"+ player + "exec PAUSE" );}},
- RESUME(3,"RESUME"){void doPlay(String player){System.out.println("player :"+ player + "exec RESUME" );}},
- STOP(4,"STOP"){void doPlay(String player){System.out.println("player :"+ player + "exec STOP" );}};
- private final int code;
- private final String name;
- SuperPlayApplyEm(int code, String name) {
- this.code = code;
- this.name = name;
- }
- @Override
- public String toString() {
- return "SuperPlayEm{" +
- "code=" + code +
- ", name='" + name + '\'' +
- '}';
- }
- abstract void doPlay(String player);
- }
- public enum MixEnum
- {
- START("400100","1001"),
- PAUSE("400100","1002"),
- RESUME("400100","1003"),
- STOP("400100","1004")
- ;
- private final String baseCode;
- private final String followCode;
- private static final Map<String,MixEnum> map = new HashMap<String,MixEnum>();
- static {
- for (MixEnum mixEnum : MixEnum.values()) {
- map.put(mixEnum.baseCode+mixEnum.followCode,mixEnum);
- }
- }
- MixEnum(String baseCode, String followCode) {
- this.baseCode = baseCode;
- this.followCode = followCode;
- }
- public static MixEnum fromCode(String code)
- {
- return map.get(code);
- }
- }
- public static void main(String[] args) {
- /*P128-P134*/
- // 传统 C 做法
- playWithInt(START); // 打印无法识别
- playWithInt(RET_OK); // 编译器不能报错
- // 普通 java 枚举
- playWithEnum(PlayEm.START);
- // playWithEnum(RET_OK);
- /**
- * 自定义 java 枚举
- */
- SuperPlayEm aaa = SuperPlayEm.START;
- playWithSuperEnum(aaa);
- /**
- * 打印所有枚举信息
- * 如果有人要一份接口文档时, 是不是会很好用
- */
- for (SuperPlayEm superPlayEm : SuperPlayEm.values()) {
- System.out.println(superPlayEm);
- }
- /**
- * 这个枚举可以做的很强大, 给他设置很多参数, 也可以定义函数
- * 但这样做可能在加一个枚举的时候, 忘记加 case 分支
- */
- aaa.doPlay("camera");
- /**
- * 防止忘记加 case
- * 但这样导致代码变多, 各有利弊吧.
- */
- SuperPlayApplyEm superPlayApplyEm = SuperPlayApplyEm.PAUSE;
- superPlayApplyEm.doPlay("camera");
- /**
- * 生成枚举的方法 1 valueOf
- */
- SuperPlayEm superPlayEm = SuperPlayEm.valueOf("START");
- System.out.println(superPlayEm);
- /**
- * 生成枚举的方法 2 自定义
- *
- */
- MixEnum mixEnum = MixEnum.fromCode("4001001002");
- System.out.println(mixEnum);
- }
- }
来源: http://www.bubuko.com/infodetail-2605846.html