使用场景
当需要一组常量时,强烈建议使用枚举类
若枚举只有一个对象, 则可以作为一种单例模式的实现方式
自定义枚举类(JDK1.5之前)
- 私有化类的构造器,保证不能在类的外部创建其对象
- 在类的内部创建枚举类的实例。声明为:public static final
- 对象如果有实例变量,应该声明为private final,并在构造器中初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Season{ private final String seasonName; private final String seasonDesc;
private Season(String seasonName,String seasonDesc){ this.seasonName = seasonName; this.seasonDesc = seasonDesc; }
public static final Season SPRING = new Season("春天","春暖花开"); public static final Season SUMMER = new Season("夏天","夏日炎炎"); public static final Season AUTUMN = new Season("秋天","秋高气爽"); public static final Season WINTER = new Season("冬天","冰天雪地");
public String getSeasonName() { return seasonName; }
public String getSeasonDesc() { return seasonDesc; } @Override public String toString() { return "Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
|
enum定义枚举类(JDK1.5之后)
●使用说明
- 使用enum定义的枚举类默认继承了java.lang.Enum类,因此不能再继承其他类
- 枚举类的构造器只能使用private权限修饰符
- 枚举类的所有实例必须显式列出(,分隔;结尾)。列出的实例系统会自动添加public static final修饰
- 必须在枚举类的第一行声明枚举类对象
- JDK1.5中可以在switch表达式中使用Enum定义的枚举类的对象作为表达式,case子句可以直接使用枚举值的名字,无需添加枚举类作为限定。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| enum Season1 { SPRING("春天","春暖花开"), SUMMER("夏天","夏日炎炎"), AUTUMN("秋天","秋高气爽"), WINTER("冬天","冰天雪地");
private final String seasonName; private final String seasonDesc;
private Season1(String seasonName,String seasonDesc){ this.seasonName = seasonName; this.seasonDesc = seasonDesc; }
public String getSeasonName() { return seasonName; }
public String getSeasonDesc() { return seasonDesc; }
}
|
Enum类的主要方法
使用enum定义枚举类之后,枚举类常用方法:(继承于java.lang.Enum类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public enum Season{ SPRING("春天","春暖花开"), SUMMER("夏天","夏日炎炎"), AUTUMN("秋天","秋高气爽"), WINTER("冬天","冰天雪地");
private final String seasonName; private final String seasonDesc;
private Season(String seasonName,String seasonDesc){ this.seasonName = seasonName; this.seasonDesc = seasonDesc; }
public String getSeasonName() { return seasonName; }
public String getSeasonDesc() { return seasonDesc; }
public static void main(String[] args) { Season summer = Season.SUMMER; System.out.println(summer.toString());
System.out.println("****************"); Season[] values = Season.values(); for(int i = 0;i < values.length;i++){ System.out.println(values[i]); } System.out.println("****************"); Season winter = Season.valueOf("WINTER");
System.out.println(winter);
}
}
|
结果
1 2 3 4 5 6 7 8
| SUMMER **************** SPRING SUMMER AUTUMN WINTER **************** WINTER
|
继承接口
使用enum定义枚举类之后,如何让枚举类对象分别实现接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| interface Info{ void show(); }
enum Season implements Info{ SPRING("春天","春暖花开"){ @Override public void show() { System.out.println("春天在哪里?"); } }, SUMMER("夏天","夏日炎炎"){ @Override public void show() { System.out.println("宁夏"); } }, AUTUMN("秋天","秋高气爽"){ @Override public void show() { System.out.println("秋天不回来"); } }, WINTER("冬天","冰天雪地"){ @Override public void show() { System.out.println("大约在冬季"); } }; }
|