Effective Java 第二版 Enum
Posted 翔鹤岭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Effective Java 第二版 Enum相关的知识,希望对你有一定的参考价值。
/**
* 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的主要内容,如果未能解决你的问题,请参考以下文章
Effective Java 英文 第二版 读书笔记 Item 10:Always override toString
Effective Java 英文 第二版 读书笔记 Item 11:Override clone judiciously
Effective Java 第三版——48. 谨慎使用流并行