码迷,mamicode.com
首页 > 编程语言 > 详细

Effective Java 第二版 Enum

时间:2018-05-18 00:27:50      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:命名   pre   res   编译   []   多参数   imp   err   防止   

/**
* 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);
}

}

Effective Java 第二版 Enum

标签:命名   pre   res   编译   []   多参数   imp   err   防止   

原文地址:https://www.cnblogs.com/aoyihuashao/p/9053959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!