java枚举详解
一、简介
public enum Day { MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY; }
一个简单的测试类:
public class TestEnum { public static void main(String[] args) { Day today=Day.FRIDAY; switch(today) { case MONDAY: System.out.println("today is monday"); break; case TUESDAY: System.out.println("today is tuesday"); break; case WEDNESDAY: System.out.println("today is webnesday"); break; case THURSDAY: System.out.println("today is thursday"); break; case FRIDAY: System.out.println("today is firday"); break; case SATURDAY: System.out.println("today is saturday"); break; case SUNDAY: System.out.println("today is sunday"); break; } } }
测试结果:
today is firday
Day枚举的本质就是一个类,编译器会自动为我们生成Day类,通过反编译得到该类如下:
final class Day extends Enum { //编译器为我们添加的静态的values()方法 public static Day[] values() { return (Day[])$VALUES.clone(); } //编译器为我们添加的静态的valueOf()方法,注意间接调用了Enum也类的valueOf方法 public static Day valueOf(String s) { return (Day)Enum.valueOf(com/zejian/enumdemo/Day, s); } //私有构造函数 private Day(String s, int i) { super(s, i); } //前面定义的7种枚举实例 public static final Day MONDAY; public static final Day TUESDAY; public static final Day WEDNESDAY; public static final Day THURSDAY; public static final Day FRIDAY; public static final Day SATURDAY; public static final Day SUNDAY; private static final Day $VALUES[]; static { //实例化枚举实例 MONDAY = new Day("MONDAY", 0); TUESDAY = new Day("TUESDAY", 1); WEDNESDAY = new Day("WEDNESDAY", 2); THURSDAY = new Day("THURSDAY", 3); FRIDAY = new Day("FRIDAY", 4); SATURDAY = new Day("SATURDAY", 5); SUNDAY = new Day("SUNDAY", 6); $VALUES = (new Day[] { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }); } }
可以清楚地看出每个枚举类型即星期数就是该Day类的一个实例对象,该构成方式和单例模式有些类似,故可以用只有一个枚举类型的枚举作为单例模式,而且枚举的构造器由编译器管理安全性十分高,既可以防止反射破解也可以防止反序列破解。
Day类继承了Enum类,下面看下Enum代码,可以更好理解枚举的方法:
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { private final String name; //枚举字符串名称 public final String name() { return name; } private final int ordinal;//枚举顺序值 public final int ordinal() { return ordinal; } //枚举的构造方法,只能由编译器调用 protected Enum(String name, int ordinal) { this.name = name; this.ordinal = ordinal; } public String toString() { return name; } public final boolean equals(Object other) { return this==other; } //比较的是ordinal值 public final int compareTo(E o) { Enum<?> other = (Enum<?>)o; Enum<E> self = this; if (self.getClass() != other.getClass() && // optimization self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal;//根据ordinal值比较大小 } @SuppressWarnings("unchecked") public final Class<E> getDeclaringClass() { //获取class对象引用,getClass()是Object的方法 Class<?> clazz = getClass(); //获取父类Class对象引用 Class<?> zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper; } public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) { //enumType.enumConstantDirectory()获取到的是一个map集合,key值就是name,value则是枚举变量值 //enumConstantDirectory是class对象内部的方法,根据class对象获取一个map集合的值 T result = enumType.enumConstantDirectory().get(name); if (result != null) return result; if (name == null) throw new NullPointerException("Name is null"); throw new IllegalArgumentException( "No enum constant " + enumType.getCanonicalName() + "." + name); } //.....省略其他没用的方法 }
二、7种常见的用法
用法一:常量
在JDK1.5 之前,我们定义常量都是: public static fianl.... 。现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。
1 public enum Color { 2 RED, GREEN, BLANK, YELLOW 3 }
用法二:switch
1 enum Signal { 2 GREEN, YELLOW, RED 3 } 4 public class TrafficLight { 5 Signal color = Signal.RED; 6 public void change() { 7 switch (color) { 8 case RED: 9 color = Signal.GREEN; 10 break; 11 case YELLOW: 12 color = Signal.RED; 13 break; 14 case GREEN: 15 color = Signal.YELLOW; 16 break; 17 } 18 } 19 }
用法三:向枚举中添加新方法
1 public enum Color { 2 RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); 3 // 成员变量 4 private String name; 5 private int index; 6 // 构造方法 7 private Color(String name, int index) { 8 this.name = name; 9 this.index = index; 10 } 11 // 普通方法 12 public static String getName(int index) { 13 for (Color c : Color.values()) { 14 if (c.getIndex() == index) { 15 return c.name; 16 } 17 } 18 return null; 19 } 20 // get set 方法 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public int getIndex() { 28 return index; 29 } 30 public void setIndex(int index) { 31 this.index = index; 32 } 33 }
用法四:覆盖枚举的方法
下面给出一个toString()方法覆盖的例子。
1 public enum Color { 2 RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); 3 // 成员变量 4 private String name; 5 private int index; 6 // 构造方法 7 private Color(String name, int index) { 8 this.name = name; 9 this.index = index; 10 } 11 //覆盖方法 12 @Override 13 public String toString() { 14 return this.index+"_"+this.name; 15 } 16 }
用法五:实现接口
所有的枚举都继承自java.lang.Enum类。由于Java 不支持多继承,所以枚举对象不能再继承其他类。
1 public interface Behaviour { 2 void print(); 3 String getInfo(); 4 } 5 public enum Color implements Behaviour{ 6 RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); 7 // 成员变量 8 private String name; 9 private int index; 10 // 构造方法 11 private Color(String name, int index) { 12 this.name = name; 13 this.index = index; 14 } 15 //接口方法 16 @Override 17 public String getInfo() { 18 return this.name; 19 } 20 //接口方法 21 @Override 22 public void print() { 23 System.out.println(this.index+":"+this.name); 24 } 25 }
用法六:使用接口组织枚举
1 public interface Food { 2 enum Coffee implements Food{ 3 BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO 4 } 5 enum Dessert implements Food{ 6 FRUIT, CAKE, GELATO 7 } 8 }
用法七:关于枚举集合的使用
java.util.EnumSet和java.util.EnumMap是两个枚举集合。EnumSet保证集合中的元素不重复;EnumMap中的 key是enum类型,而value则可以是任意类型。关于这个两个集合的使用就不在这里赘述。