1. 程式人生 > >Effective Java 第二版 Enum

Effective Java 第二版 Enum

定義 override mce java name map base @override port

/**
* 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