分类: Java
2015-08-06 14:36:05
enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中。
public enum EnumTest { MON, TUE, WED, THU, FRI, SAT, SUN; } |
这段代码实际上调用了7次 Enum(String name, int ordinal):
new Enum<EnumTest>("MON",0); new Enum<EnumTest>("TUE",1); new Enum<EnumTest>("WED",2); ... ... |
可以用if/else、switch来遍历
for (EnumTest e : EnumTest.values()) {
System.out.println(e.toString());
}
遍历所有定义的value。
enum 的语法结构尽管和 class 的语法不一样,但是经过编译器编译之后产生的是一个class文件。该class文件经过反编译可以看到实际上是生成了一个类,该类继承了java.lang.Enum<E>。
enum 不能使用 extends 关键字继承其他类,因为 enum 已经继承了 java.lang.Enum(java是单一继承)。
枚举类型带参数,必须要对应匹配的构造方法
2. 可以创建一个enum类,把它看做一个普通的类。除了它不能继承其他类了。(java是单继承,它已经继承了Enum),
可以添加其他方法,覆盖它本身的方法
3. switch()参数可以使用enum了
4. values()方法是编译器插入到enum定义中的static方法,所以,当你将enum实例向上转型为父类Enum是,values()就不可访问了。解决办法:在Class中有一个getEnumConstants()方法,所以即便Enum接口中没有values()方法,我们仍然可以通过Class对象取得所有的enum实例
5. 无法从enum继承子类,如果需要扩展enum中的元素,在一个接口的内部,创建实现该接口的枚举,以此将元素进行分组。达到将枚举元素进行分组。
6. 使用EnumSet代替标志。enum要求其成员都是唯一的,但是enum中不能删除添加元素。
7. EnumMap的key是enum,value是任何其他Object对象。
8. enum允许程序员为eunm实例编写方法。所以可以为每个enum实例赋予各自不同的行为。
9. 使用enum的职责链(Chain of Responsibility) .这个关系到设计模式的职责链模式。以多种不同的方法来解决一个问题。然后将他们链接在一起。当一个请求到来时,遍历这个链,直到链中的某个解决方案能够处理该请求。
10. 使用enum的状态机
11. 使用enum多路分发
最后来个完整的实例:
public class soundModeDemo
{
/**
* 将用enum枚举去除以下方法中的switch/case
*/
public void processAlarmEvents(AlarmEvent[] events)
{
AlarmEvent lastEvent = events[events.length - 1];
byte severity = lastEvent.getAlarmData().getPerceivedSeverity();
switch (lastEvent.getEventType())
{
case AlarmEvent.NEW_ALARM:
if (severity != PerceivedSeverity.SEVERITY_CLEARED)
{
System.out.println("playAlarmSound:"+severity);
}
break;
case AlarmEvent.DELETE_ALARM:
if (severity == PerceivedSeverity.SEVERITY_CLEARED)
{
return;
}
System.out.println("playClearSound:"+severity);
break;
case AlarmEvent.SEVERITY_CHANGED:
byte oldSeverity = lastEvent.getOriginalAlarmData().getPerceivedSeverity();
if ((oldSeverity == PerceivedSeverity.SEVERITY_CLEARED))
{
System.out.println("playAlarmSound:"+severity);
}
else if (severity == PerceivedSeverity.SEVERITY_CLEARED)
{
System.out.println("playClearSound:"+severity);
}
break;
}
}
//带有构造方法的枚举实现
public enum SoundPlay{
NEW_ALARM(AlarmEvent.NEW_ALARM, PerceivedSeverity.SEVERITY_CLEARED, (byte)0),
DELETE_ALARM(AlarmEvent.DELETE_ALARM, PerceivedSeverity.SEVERITY_CLEARED, (byte)0),
SEVERITY_CHANGED(AlarmEvent.SEVERITY_CHANGED, PerceivedSeverity.SEVERITY_CLEARED, (byte)0);
private final byte perceivedSeverity = PerceivedSeverity.SEVERITY_CLEARED;
private byte eventType;
private byte severity;
private byte oldSeverity;
public void setSeverity(byte severity)
{
this.severity = severity;
}
public void setOldSeverity(byte oldSeverity)
{
this.oldSeverity = oldSeverity;
}
Private SoundPlay(byte eventType, byte severity, byte oldSeverity){
this.eventType = eventType;
this.severity = severity;
this.oldSeverity = oldSeverity;
}
public void printAllValues() {
System.out.println("eventType:"+this.eventType+
" severity:"+this.severity+
" oldSeverity:"+this.oldSeverity);
}
public void doPlay(){
if(this.eventType == AlarmEvent.NEW_ALARM)playNewAlarm();
if(this.eventType == AlarmEvent.DELETE_ALARM)playDeleteAlarm();
if(this.eventType == AlarmEvent.SEVERITY_CHANGED)playSeverityChange();
}
private void playSeverityChange()
{
if (this.oldSeverity == perceivedSeverity){
System.out.println("playAlarmSound:" + this.oldSeverity);
}else if (this.severity == perceivedSeverity){
System.out.println("playClearSound:" + this.severity);
}
}
private void playDeleteAlarm()
{
if (this.severity == perceivedSeverity){
return;
}else{
System.out.println("playClearSound:" + this.severity);
}
}
private void playNewAlarm()
{
if (this.severity != perceivedSeverity){
System.out.println("playAlarmSound:" + this.severity);
}
}
}
public static void main(String[] args)
{
//每次调用,都需要设置severity或oldSeverity
SoundPlay soundPlay1 = SoundPlay.NEW_ALARM;
soundPlay1.setSeverity((byte)4);
soundPlay1.printAllValues();
soundPlay1.doPlay();
}
}
//最常用标准的枚举方式
public enum SoundPlay{
NEW_ALARM{
@Override
public void play(AlarmEvent event)
{
if (event.getAlarmData().getPerceivedSeverity() != PerceivedSeverity.SEVERITY_CLEARED){
System.out.println("playAlarmSound:" + event.getAlarmData().getPerceivedSeverity());
}
}
@Override
protected byte getType()
{
return AlarmEvent.NEW_ALARM;
}
},
DELETE_ALARM{
@Override
public void play(AlarmEvent event)
{
if (event.getAlarmData().getPerceivedSeverity() == PerceivedSeverity.SEVERITY_CLEARED){
return;
}else{
System.out.println("playClearSound:" + event.getAlarmData().getPerceivedSeverity());
}
}
@Override
protected byte getType()
{
return AlarmEvent.DELETE_ALARM;
}
},
SEVERITY_CHANGED {
@Override
public void play(AlarmEvent event)
{
if (event.getAlarmData().getPerceivedSeverity() == PerceivedSeverity.SEVERITY_CLEARED){
System.out.println("playAlarmSound:" + event.getOriginalAlarmData().getPerceivedSeverity());
}else if (event.getAlarmData().getPerceivedSeverity() == PerceivedSeverity.SEVERITY_CLEARED){
System.out.println("playClearSound:" + event.getAlarmData().getPerceivedSeverity());
}
}
@Override
protected byte getType()
{
return AlarmEvent.DELETE_ALARM;
}
},
DEFAULT{
@Override
public void play(AlarmEvent event)
{
System.out.println("没有匹配的枚举值内容,调用的默认方法!");
}
@Override
protected byte getType()
{
return -1;
}
};
public abstract void play(AlarmEvent event);
protected abstract byte getType();
public static SoundPlay valueOf(byte eventType){
for(SoundPlay soundPlay:SoundPlay.values()){
if(eventType==soundPlay.getType()){
return soundPlay;
}
}
return DEFAULT;
}
}