【Java】-NO.16.EBook.4.Java.1.005-【疯狂Java讲义第3版 李刚】- 枚举

1.0.0 Summary

Tittle:【Java】-NO.16.EBook.4.Java.1.005-【疯狂Java讲义第3版 李刚】- 枚举

Style:EBook

Series:Java

Since:2017-09-18

End:....

Total Hours:...

Degree Of Diffculty:2

Degree Of Mastery:2

Practical Level:2

Desired Goal:2

Archieve Goal:....

Gerneral Evaluation:...

Writer:kingdelee

Related Links:

http://www.cnblogs.com/kingdelee/

package com.lee.test.java.ebook.crazy_java.u_6_oop_2.c_6_9_emnu;

public class MyEmunTest {

    // 1. 最简单的枚举类定义方式
enum Season {
SPRING, SUMMER, FALL, WINTER;
} // 2. 定义枚举时,应把成员变量赋值为 final 以免可以直接通过 .var赋值修改
// 2.1 枚举应有构造方法指定成员变量,其枚举对象隐式为 public static final 类型 = new ..("");
// 2.2 枚举类中的枚举值实际上是匿名子类的实例
enum Gender {
MALE("男"), FAMALE("女");
private final String name; Gender(String name) {
this.name = name;
}
} interface Show{
void show();
} // 2.3 枚举类实现接口时,每个枚举值都要实现接口
enum AA implements Show{
SPRING {
// 2.3 枚举类中定义的抽象方法,每个枚举值都需要实现
@Override
public void mustAction() {
System.out.println("mustAction");
} // 2.3 枚举类实现接口时,每个枚举值都要实现接口
@Override
public void show() {
System.out.println("show");
}
}, SUMMER {
// 2.3 枚举类中定义的抽象方法,每个枚举值都需要实现
@Override
public void mustAction() {
System.out.println("mustAction");
} // 2.3 枚举类实现接口时,每个枚举值都要实现接口
@Override
public void show() {
System.out.println("show");
}
}; // 2.3 枚举类中定义的抽象方法,每个枚举值都需要实现
public abstract void mustAction();
} public static void main(String[] args) { for (Season season : Season.values()) {
if (Season.SPRING == season) {
System.out.println(season.name());
}
} Gender male = Enum.valueOf(Gender.class, "MALE");
// 报错
// Gender male = Enum.valueOf(Gender.class, "男");
System.out.println(male == Gender.MALE); System.out.println(male.name); AA spring = AA.SPRING; } }

  

import java.util.HashMap;
import java.util.Map; /**
* 模拟星期中的表示的天,每个星期天都表示一个对象
* 1、类中的每一个枚举成员都是该类的一个实例对象
* 2、构造函数私有化
* 3、提供操作枚举成员的抽象方法和静态方法
*/
public abstract class WeekDate {
/**
* 星期一
*/
public static final WeekDate MON = new WeekDate("MON",0) {//匿名子类
@Override
public WeekDate nextDay() {
return TUES;
}
@Override
public WeekDate preDay() {
return SUN;
}
@Override
public String toString() {
return "WeekDate.MON";
}
}; /**
* 星期二
*/
public static final WeekDate TUES = new WeekDate("TUES",1) {
@Override
public WeekDate nextDay() {
return WEDNES;
}
@Override
public WeekDate preDay() {
return MON;
}
@Override
public String toString() {
return "WeekDate.TUES";
}
}; /**
* 星期三
*/
public static final WeekDate WEDNES = new WeekDate("WEDNES",2) {
@Override
public WeekDate nextDay() {
return THURS;
}
@Override
public WeekDate preDay() {
return TUES;
}
@Override
public String toString() {
return "WeekDate.WEDNES";
}
}; /**
* 星期四
*/
public static final WeekDate THURS = new WeekDate("THURS",3) {
@Override
public WeekDate nextDay() {
return FRI;
}
@Override
public WeekDate preDay() {
return WEDNES;
}
@Override
public String toString() {
return "WeekDate.THURS";
}
}; /**
* 星期五
*/
public static final WeekDate FRI = new WeekDate("FRI",4){
@Override
public WeekDate nextDay() {
return SATUR;
}
@Override
public WeekDate preDay() {
return THURS;
}
@Override
public String toString() {
return "WeekDate.FRI";
}
}; /**
* 星期六
*/
public static final WeekDate SATUR = new WeekDate("SATUR",5){
@Override
public WeekDate nextDay() {
return SUN;
}
@Override
public WeekDate preDay() {
return FRI;
}
@Override
public String toString() {
return "WeekDate.SATUR";
}
}; /**
* 星期日
*/
public static final WeekDate SUN = new WeekDate("SUN",6){
@Override
public WeekDate nextDay() {
return MON;
}
@Override
public WeekDate preDay() {
return SATUR;
}
@Override
public String toString() {
return "WeekDate.SUN";
}
}; private static Map<String, WeekDate> valueMap = new HashMap<String, WeekDate>(); /**
* 枚举名称
*/
private final String name; /**
* 枚举成员的顺序
*/
private final int ordinal; private WeekDate(String name,int ordinal) {
this.name = name;
this.ordinal = ordinal;
} /**
* 保存枚举成员
*/
private static WeekDate[] values = {
MON,TUES,WEDNES,THURS,FRI,SATUR,SUN
}; //初始化
static {
valueMap.put("MON", values[0]);
valueMap.put("TUES", values[1]);
valueMap.put("WEDNES", values[2]);
valueMap.put("THURS", values[3]);
valueMap.put("FRI", values[4]);
valueMap.put("SATUR", values[5]);
valueMap.put("SUN", values[6]);
} /**
* 下一天
* @return
*/
public abstract WeekDate nextDay(); /**
* 前一天
* @return
*/
public abstract WeekDate preDay(); /**
* 枚举中的所有成员
* @return
*/
public static WeekDate[] values() {
return values;
} /**
* 将一个字符串转换成一个枚举成员对象
* @param name 枚举名称
* @return 枚举对象
*/
public static WeekDate valueOf(String name) {
if (name.equalsIgnoreCase("MON")) {
return MON;
} else if (name.equalsIgnoreCase("TUES")) {
return TUES;
} else if (name.equalsIgnoreCase("WEDES")) {
return WEDNES;
} else if (name.equalsIgnoreCase("THURS")) {
return THURS;
} else if (name.equalsIgnoreCase("FRI")) {
return FRI;
} else if (name.equalsIgnoreCase("SATUR")) {
return SATUR;
} else if (name.equalsIgnoreCase("SUN")) {
return SUN;
} else {
throw new IllegalArgumentException("找不到" + name + "枚举类型!");
}
} /**
* 优化字符串转枚举对象
* @param name 枚举名称
* @return 枚举对象
*/
public static WeekDate valueOf_2(String name) {
WeekDate value = valueMap.get(name.toUpperCase());
if (value == null) {
throw new IllegalArgumentException("找不到" + name + "枚举类型!");
}
return value;
}
public String getName() {
return name;
}
public int getOrdinal() {
return ordinal;
}
}

  

上一篇:功能强大支持64位操作系统的转Flash软件(doc转swf):Print2Flash


下一篇:Azure File SMB3.0文件共享服务(4)