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第二版》总结
		第1条:考虑用静态工厂方法代替构造器 通常我们会使用 构造方法 来实例化一个对象,例如: // 对象定义 public class Student{ // 姓名 private String name ... 
- 《Effective Java 第二版》读书笔记
		想成为更优秀,更高效程序员,请阅读此书.总计78个条目,每个对应一个规则. 第二章 创建和销毁对象 一,考虑用静态工厂方法代替构造器 二, 遇到多个构造器参数时要考虑用builder模式 /** * ... 
- 如何创建和销毁对象(Effective Java 第二章)
		最近有在看Effective Java,特此记录下自己所体会到的东西,写篇博文会更加的加深印象,如有理解有误的地方,希望不吝赐教. 这章主题主要是介绍:何时以及如何创建对象,何时以及如何避免创建对象, ... 
- 对于所有对象都通用方法的解读(Effective Java 第二章)
		这篇博文主要介绍覆盖Object中的方法要注意的事项以及Comparable.compareTo()方法. 一.谨慎覆盖equals()方法 其实平时很少要用到覆盖equals方法的情况,没有什么特殊 ... 
- [Effective Java]第二章 创建和销毁对象
		声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ... 
- effective java——30使用enum
		1, 枚举太阳系八大行星 package com.enum30.www; public enum Planet {//枚举太阳系八大行星 MERCURY(3.302e+23,2.439e6), VEN ... 
- Effective C++ 第二版 17)operator=检查自己 18)接口完整 19)成员和友元函数
		条款17 在operator=中检查给自己赋值的情况 1 2 3 class X { ... }; X a; a = a; // a 赋值给自己 >赋值给自己make no sense, 但 ... 
- Effective C++ 第二版 1)const和inline 2)iostream
		条款1 尽量用const和inline而不用#define >"尽量用编译器而不用预处理" Ex. #define ASPECT_R 1.653 编译器永远不会看到AS ... 
- Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承
		条款40 通过分层来体现"有一个"或"用...来实现" 使某个类的对象成为另一个类的数据成员, 实现将一个类构筑在另一个类之上, 这个过程称为 分层Layeri ... 
随机推荐
- 案例25-servlet的抽取
			1 product模块的抽取版本一 1 ProductServlet代码 抽取之后,原来对应的IndexServlet,ProductListByCidServlet等都可以删除.对应的web.xml ... 
- 【CSS】 布局之圣杯布局
			在看众多大神的css布局指南时,经常看到一个布局:圣杯布局(也有称为双飞翼布局的).今天我们也来剖析一下. 其实,对于众多css布局,我们只要明确理解了3种技术,那么基本上大多数布局都难不倒我们了: ... 
- input输入提示历史记录
			一般便于用户的输入习惯,我们都会提示历史消息,让用户有更好的使用体验,以前可能比较多朋友会用js来实现,现在HTML5的datalist可以轻松帮我们实现这个功能!只需以下几行代码 <!doct ... 
- VS 同词高亮显示
			Visual Studio 2010 选中高亮插件 - Highlight all occurrences of selected word Highlight all occurrences of ... 
- NPM 与前端包管理
			我们很清楚,前端资源及其依赖管理一直是 npm 的重度使用场景,同时这也一直是 Node.js 普及的重要推动力.但这类应用场景到底有多重度?这是一个很难回答的问题.这份 “npm 最常下载的包的清单 ... 
- jdk各版本
			1.jdk1.7: 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头: 1.2 Switch语句支持string类型: 2.jdk1.8: 
- shodan在渗透测试中的应用
			场景1:想搜索美国所有的elasticsearch服务器 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.设计 ... 
- Effective C++ .15,16获取原始资源和成对使用同类型new和delete
			15. 智能指针可以通过get操作 #include <iostream> #include <cstdlib> #include <memory> using n ... 
- Axios介绍和使用
			一.介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 官方资料和介绍 从浏览器中创建 XMLHttpRequests 从 node.js 创建 h ... 
- Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: Unable to load the mojo 'resources' (or one of its required components)
			1.异常提示: Description Resource Path Location Type Execution default-resources of goal org.apache.maven ... 
