java 的 enum 类型

2016-05-20 18:36:26   最后更新: 2016-05-20 18:36:26   访问数量:596




在很多语言中,枚举都是一个非常实用的类型,它提供了一个常量的集合,让我们更加方便的管理代码中的常量

java 在 1.5 以后也提供了 enum 类型

在 java1.5 以前,如果使用类似的功能,通常需要定义常量 class,通过类名来区分是否是常量的封装类,同时将所有字段都显式定义为 public static final 类型

public class State { public static final int ON = 1; public static final Integer OFF= 0; }

 

 

java1.5 以后提供的 enum 类型中,隐式定义所有字段必须为 public static final,这样,枚举类型的定义就显得简洁而高效

下面就来展示一下枚举类型的用法

 

  • WeekEnum.java
package com.techlog.test.service; /** * just for test * Created by techlog on 16/5/20. */ public enum WeekEnum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

 

 

  • ForTest.java
package com.techlog.test.service; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public class ForTest { public static void main(String[] argv) { WeekEnum weekEnum = WeekEnum.FRIDAY; out.println("Today is " + weekEnum + ", and the number of today is " + weekEnum.ordinal()); } }

 

 

打印出了:

Today is FRIDAY, and the number of today is 5

 

  • 需要注意的是,enum 类型作为一个特殊的类,他除了所有成员都是隐式 public static final 外,它本身也是隐式 final 的,因此 enum 类型是无法被继承的
  • 同时,enum 类型隐式继承自 Enum 对象,由于 java 单继承的特性,enum 也因此不能再继承自任何其他类,但下文会介绍,enum 类型可以实现接口,也可以定义任何方法

 

  • WeekEnum.java
package com.techlog.test.service; /** * just for test * Created by techlog on 16/5/20. */ public enum WeekEnum { SUNDAY("sunday", 0, false), MONDAY("monday", 1, true), TUESDAY("tuesday", 2, true), WEDNESDAY("wednesday", 3, true), THURSDAY("thursday", 4, true), FRIDAY("friday", 5, true), SATURDAY("saturday", 6, false); private String day; private int index; private boolean workday; public String getDay() { return day; } public int getIndex() { return index; } public boolean isWorkday() { return workday; } WeekEnum(String day, int index, boolean workday) { this.day = day; this.index = index; this.workday = workday; } }

 

 

  • ForTest.java
package com.techlog.test.service; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public class ForTest { public static void main(String[] argv) { WeekEnum weekEnum = WeekEnum.FRIDAY; String workday = weekEnum.isWorkday() ? "a work day" : "not a work day"; out.println("Today is " + weekEnum.getDay() + ", and the number of today is " + weekEnum.getIndex() + ", and today is " + workday + "."); } }

 

 

打印出了:

Today is friday, and the number of today is 5, and today is a work day.

 

这里我们没有使用 Enum 类型给我们提供的默认编号,而是使用了一个自定义的字段 index,同时提供了标记名称的字段 day,以及判断是否是工作日的 workday 字段

可以看到,使用额外的字段必须要添加相应的构造器,同时为了获取相应的值,需要相应的 get 方法

枚举类型都是隐式 static final 的,因此我们不能从外部改变他

 

事实上,枚举类型最常用的用法是在 switch 语句中:

package com.techlog.test.service; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public class ForTest { public static void main(String[] argv) { WeekEnum weekEnum = WeekEnum.MONDAY; switch (weekEnum) { case MONDAY: case TUESDAY: out.println("begin to a week"); break; case WEDNESDAY: case THURSDAY: out.println("in a week"); break; case FRIDAY: out.println("close to the weekend"); break; case SATURDAY: case SUNDAY: out.println("have fun on the weekend"); break; } } }

 

 

foreach

有时我们也需要遍历枚举类型中的元素,这时,使用 java1.5 提供的 foreach 循环就会方便很多:

package com.techlog.test.service; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public class ForTest { public static void main(String[] argv) { int weekIndex = 5; for (WeekEnum week : WeekEnum.values()) { if (week.ordinal() == weekIndex) { if (week.isWorkday()) { out.println("get down to the work"); } else { out.println("have fun on the weekend"); } } } } }

 

 

EnumSet 的遍历

java1.5 同时还提供了 EnumSet 和 EnumMap 用来存储和操作枚举类型,他们除了提供存储外,还提供了很多 static 方法供静态调用

如用于创建 EnumSet 的 allOf 方法,下面我们使用这个方法完成枚举类型的遍历

package com.techlog.test.service; import java.util.EnumSet; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public class ForTest { public static void main(String[] argv) { int weekIndex = 5; for (WeekEnum week : EnumSet.allOf(WeekEnum.class)) { if (week.ordinal() == weekIndex) { if (week.isWorkday()) { out.println("get down to the work"); } else { out.println("have fun on the weekend"); } } } } }

 

 

部分遍历

EnumSet 实现的 rang() 方法就支持取出一个连续的部分组成一个新的 EnumSet 集合对象

package com.techlog.test.service; import java.util.EnumSet; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public class ForTest { public static void main(String[] argv) { int weekIndex = 5; for (WeekEnum week : EnumSet.range(WeekEnum.FRIDAY, WeekEnum.SUNDAY)) { if (week.ordinal() == weekIndex) { if (week.isWorkday()) { out.println("get down to the work"); } else { out.println("have fun on the weekend"); } } } } }

 

 

同时,java 也允许枚举类型实现某个接口,从而为枚举类型提供了更大的灵活性

package com.techlog.test.service; import static java.lang.System.out; /** * just for test * Created by techlog on 16/5/20. */ public enum WeekEnum implements WorkInterface { SUNDAY("sunday", 0, false), MONDAY("monday", 1, true), TUESDAY("tuesday", 2, true), WEDNESDAY("wednesday", 3, true), THURSDAY("thursday", 4, true), FRIDAY("friday", 5, true), SATURDAY("saturday", 6, false); private String day; private int index; private boolean workday; public String getDay() { return day; } public int getIndex() { return index; } public boolean isWorkday() { return workday; } @Override public void doSomeWork(String work) { if (workday) { out.println("doing " + work); } else { out.println("plan to do " + work); } } WeekEnum(String day, int index, boolean workday) { this.day = day; this.index = index; this.workday = workday; } } interface WorkInterface { void doSomeWork(String work); }

 

 






技术帖      龙潭书斋      java      枚举类型      enum      enumeration      枚举      常量      java5      j2ee     


京ICP备15018585号