Java(enum)枚举用法详解

原文地址: https://blog.csdn.net/moakun/article/details/80554066


概念

enum的全称为 enumeration, 是 JDK 1.5 中引入的新特性。

在Java中,被 enum 关键字修饰的类型就是枚举类型。形式如下:

[java]  view plain  copy
  1. enum Color { RED, GREEN, BLUE }  

如果枚举不添加任何方法,枚举值默认为从0开始的有序数值。以 Color 枚举类型举例,它的枚举常量依次为RED:0,GREEN:1,BLUE:2

枚举的好处:可以将常量组织起来,统一进行管理。

枚举的典型应用场景:错误码、状态机等。

枚举类型的本质

尽管enum 看起来像是一种新的数据类型,事实上,enum是一种受限制的类,并且具有自己的方法。

创建enum时,编译器会为你生成一个相关的类,这个类继承自 java.lang.Enum

java.lang.Enum类声明

[java]  view plain  copy
  1. public abstract class Enum<E extends Enum<E>>  
  2.     implements Comparable<E>, Serializable { ... }  

枚举的方法

在enum中,提供了一些基本方法:

values():返回enum实例的数组,而且该数组中的元素严格保持在enum中声明时的顺序。

name():返回实例名。

ordinal():返回实例声明时的次序,从0开始。

getDeclaringClass():返回实例所属的enum类型。

equals():判断是否为同一个对象。

可以使用 == 来比较enum实例。

此外,java.lang.Enum实现了Comparable和 Serializable 接口,所以也提供 compareTo() 方法。

[java]  view plain  copy
  1. public class EnumMethodDemo {  
  2.   enum Color {RED, GREEN, BLUE;}  
  3.   enum Size {BIG, MIDDLE, SMALL;}  
  4.   public static void main(String args[]) {  
  5.     System.out.println("=========== Print all Color ===========");  
  6.     for (Color c : Color.values()) {  
  7.       System.out.println(c + " ordinal: " + c.ordinal());  
  8.     }  
  9.     System.out.println("=========== Print all Size ===========");  
  10.     for (Size s : Size.values()) {  
  11.       System.out.println(s + " ordinal: " + s.ordinal());  
  12.     }  
  13.   
  14.     Color green = Color.GREEN;  
  15.     System.out.println("green name(): " + green.name());  
  16.     System.out.println("green getDeclaringClass(): " + green.getDeclaringClass());  
  17.     System.out.println("green hashCode(): " + green.hashCode());  
  18.     System.out.println("green compareTo Color.GREEN: " + green.compareTo(Color.GREEN));  
  19.     System.out.println("green equals Color.GREEN: " + green.equals(Color.GREEN));  
  20.     System.out.println("green equals Size.MIDDLE: " + green.equals(Size.MIDDLE));  
  21.     System.out.println("green equals 1: " + green.equals(1));  
  22.     System.out.format("green == Color.BLUE: %b\n", green == Color.BLUE);  
  23.   }  
  24. }  

输出

[plain]  view plain  copy
  1. =========== Print all Color ===========  
  2. RED ordinal: 0  
  3. GREEN ordinal: 1  
  4. BLUE ordinal: 2  
  5. =========== Print all Size ===========  
  6. BIG ordinal: 0  
  7. MIDDLE ordinal: 1  
  8. SMALL ordinal: 2  
  9. green name(): GREEN  
  10. green getDeclaringClass(): class org.zp.javase.enumeration.EnumDemo$Color  
  11. green hashCode(): 460141958  
  12. green compareTo Color.GREEN: 0  
  13. green equals Color.GREEN: true  
  14. green equals Size.MIDDLE: false  
  15. green equals 1: false  
  16. green == Color.BLUE: false  

枚举的特性

枚举的特性,归结起来就是一句话:

除了不能继承,基本上可以将enum 看做一个常规的类。

但是这句话需要拆分去理解,让我们细细道来。

枚举可以添加方法

在概念章节提到了,枚举值默认为从0开始的有序数值 。那么问题来了:如何为枚举显示的赋值。

Java 不允许使用 = 为枚举常量赋值

如果你接触过C/C++,你肯定会很自然的想到赋值符号 = 。在C/C++语言中的enum,可以用赋值符号=显示的为枚举常量赋值;但是 ,很遗憾,Java 语法中却不允许使用赋值符号 = 为枚举常量赋值。

例:C/C++ 语言中的枚举声明

[cpp]  view plain  copy
  1. typedef enum{  
  2.   ONE = 1,  
  3.   TWO,  
  4.   THREE = 3,  
  5.   TEN = 10  
  6. } Number;  

enum 可以添加普通方法、静态方法、抽象方法、构造方法

Java虽然不能直接为实例赋值,但是它有更优秀的解决方案:为 enum 添加方法来间接实现显示赋值。

创建 enum 时,可以为其添加多种方法,甚至可以为其添加构造方法。

注意一个细节:如果要为enum定义方法,那么必须在enum的最后一个实例尾部添加一个分号。此外,在enum中,必须先定义实例,不能将字段或方法定义在实例前面。否则,编译器会报错。

例:全面展示如何在枚举中定义普通方法、静态方法、抽象方法、构造方法

[java]  view plain  copy
  1. public enum ErrorCode {  
  2.   OK(0) {  
  3.     public String getDescription() {  
  4.       return "成功";  
  5.     }  
  6.   },  
  7.   ERROR_A(100) {  
  8.     public String getDescription() {  
  9.       return "错误A";  
  10.     }  
  11.   },  
  12.   ERROR_B(200) {  
  13.     public String getDescription() {  
  14.       return "错误B";  
  15.     }  
  16.   };  
  17.   
  18.   private int code;  
  19.     
  20.   // 构造方法:enum的构造方法只能被声明为private权限或不声明权限  
  21.   private ErrorCode(int number) { // 构造方法  
  22.     this.code = number;  
  23.   }  
  24.   public int getCode() { // 普通方法  
  25.     return code;  
  26.   } // 普通方法  
  27.   public abstract String getDescription(); // 抽象方法  
  28.   public static void main(String args[]) { // 静态方法  
  29.     for (ErrorCode s : ErrorCode.values()) {  
  30.       System.out.println("code: " + s.getCode() + ", description: " + s.getDescription());  
  31.     }  
  32.   }  
  33. }  

注:上面的例子并不可取,仅仅是为了展示枚举支持定义各种方法。下面是一个简化的例子

例:一个错误码枚举类型的定义

本例和上例的执行结果完全相同。

[java]  view plain  copy
  1. public enum ErrorCodeEn {  
  2.   OK(0"成功"),  
  3.   ERROR_A(100"错误A"),  
  4.   ERROR_B(200"错误B");  
  5.   
  6.   ErrorCodeEn(int number, String description) {  
  7.     this.code = number;  
  8.     this.description = description;  
  9.   }  
  10.   private int code;  
  11.   private String description;  
  12.   public int getCode() {  
  13.     return code;  
  14.   }  
  15.   public String getDescription() {  
  16.     return description;  
  17.   }  
  18.   public static void main(String args[]) { // 静态方法  
  19.     for (ErrorCodeEn s : ErrorCodeEn.values()) {  
  20.       System.out.println("code: " + s.getCode() + ", description: " + s.getDescription());  
  21.     }  
  22.   }  
  23. }  

枚举可以实现接口

enum 可以像一般类一样实现接口。

同样是实现上一节中的错误码枚举类,通过实现接口,可以约束它的方法。

[java]  view plain  copy
  1. public interface INumberEnum {  
  2.   int getCode();  
  3.   String getDescription();  
  4. }  
  5.   
  6. public enum ErrorCodeEn2 implements INumberEnum {  
  7.   OK(0"成功"),  
  8.   ERROR_A(100"错误A"),  
  9.   ERROR_B(200"错误B");  
  10.   
  11.   ErrorCodeEn2(int number, String description) {  
  12.     this.code = number;  
  13.     this.description = description;  
  14.   }  
  15.     
  16.   private int code;  
  17.   private String description;  
  18.   
  19.   @Override  
  20.   public int getCode() {  
  21.     return code;  
  22.   }  
  23.   
  24.   @Override  
  25.   public String getDescription() {  
  26.     return description;  
  27.   }  
  28. }  

枚举不可以继承

enum 不可以继承另外一个类,当然,也不能继承另一个 enum 。

因为 enum 实际上都继承自 java.lang.Enum 类,而 Java 不支持多重继承,所以enum不能再继承其他类,当然也不能继承另一个 enum

枚举的应用场景

组织常量

在JDK1.5 之前,在Java中定义常量都是public static final TYPE a; 这样的形式。有了枚举,你可以将有关联关系的常量组织起来,使代码更加易读、安全,并且还可以使用枚举提供的方法。

枚举声明的格式

注:如果枚举中没有定义方法,也可以在最后一个实例后面加逗号、分号或什么都不加。

下面三种声明方式是等价的:

[java]  view plain  copy
  1. enum Color { RED, GREEN, BLUE }  
  2. enum Color { RED, GREEN, BLUE, }  
  3. enum Color { RED, GREEN, BLUE; }  

switch 状态机

我们经常使用switch语句来写状态机。JDK7以后,switch已经支持 int、char、String、enum 类型的参数。这几种类型的参数比较起来,使用枚举的switch代码更具有可读性。

[java]  view plain  copy
  1. enum Signal {RED, YELLOW, GREEN}  
  2.   
  3. public static String getTrafficInstruct(Signal signal) {  
  4.   String instruct = "信号灯故障";  
  5.   switch (signal) {  
  6.     case RED:  
  7.       instruct = "红灯停";  
  8.       break;  
  9.     case YELLOW:  
  10.       instruct = "黄灯请注意";  
  11.       break;  
  12.     case GREEN:  
  13.       instruct = "绿灯行";  
  14.       break;  
  15.     default:  
  16.       break;  
  17.   }  
  18.   return instruct;  
  19. }  

组织枚举

可以将类型相近的枚举通过接口或类组织起来。

但是一般用接口方式进行组织。

原因是:Java接口在编译时会自动为enum类型加上public static修饰符;Java类在编译时会自动为 enum 类型加上static修饰符。看出差异了吗?没错,就是说,在类中组织 enum,如果你不给它修饰为 public,那么只能在本包中进行访问。

例:在接口中组织 enum

[java]  view plain  copy
  1. public interface Plant {  
  2.   enum Vegetable implements INumberEnum {  
  3.     POTATO(0"土豆"),  
  4.     TOMATO(0"西红柿");  
  5.   
  6.     Vegetable(int number, String description) {  
  7.       this.code = number;  
  8.       this.description = description;  
  9.     }  
  10.   
  11.     private int code;  
  12.     private String description;  
  13.   
  14.     @Override  
  15.     public int getCode() {  
  16.       return 0;  
  17.     }  
  18.   
  19.     @Override  
  20.     public String getDescription() {  
  21.       return null;  
  22.     }  
  23.   }  
  24.   
  25.   enum Fruit implements INumberEnum {  
  26.     APPLE(0"苹果"),  
  27.     ORANGE(0"桔子"),  
  28.     BANANA(0"香蕉");  
  29.   
  30.     Fruit(int number, String description) {  
  31.       this.code = number;  
  32.       this.description = description;  
  33.     }  
  34.   
  35.     private int code;  
  36.     private String description;  
  37.   
  38.     @Override  
  39.     public int getCode() {  
  40.       return 0;  
  41.     }  
  42.   
  43.     @Override  
  44.     public String getDescription() {  
  45.       return null;  
  46.     }  
  47.   }  
  48. }  

例:在类中组织 enum

本例和上例效果相同。

[java]  view plain  copy
  1. public class Plant2 {  
  2.   public enum Vegetable implements INumberEnum {...} // 省略代码  
  3.   public enum Fruit implements INumberEnum {...} // 省略代码  
  4. }  

策略枚举

EffectiveJava中展示了一种策略枚举。这种枚举通过枚举嵌套枚举的方式,将枚举常量分类处理。

这种做法虽然没有switch语句简洁,但是更加安全、灵活。

例:EffectvieJava中的策略枚举范例

[java]  view plain  copy
  1. enum PayrollDay {  
  2.   MONDAY(PayType.WEEKDAY), TUESDAY(PayType.WEEKDAY), WEDNESDAY(  
  3.       PayType.WEEKDAY), THURSDAY(PayType.WEEKDAY), FRIDAY(PayType.WEEKDAY), SATURDAY(  
  4.       PayType.WEEKEND), SUNDAY(PayType.WEEKEND);  
  5.   
  6.   private final PayType payType;  
  7.   
  8.   PayrollDay(PayType payType) {  
  9.     this.payType = payType;  
  10.   }  
  11.   
  12.   double pay(double hoursWorked, double payRate) {  
  13.     return payType.pay(hoursWorked, payRate);  
  14.   }  
  15.   
  16.   // 策略枚举  
  17.   private enum PayType {  
  18.     WEEKDAY {  
  19.       double overtimePay(double hours, double payRate) {  
  20.         return hours <= HOURS_PER_SHIFT ? 0 : (hours - HOURS_PER_SHIFT)  
  21.             * payRate / 2;  
  22.       }  
  23.     },  
  24.     WEEKEND {  
  25.       double overtimePay(double hours, double payRate) {  
  26.         return hours * payRate / 2;  
  27.       }  
  28.     };  
  29.     private static final int HOURS_PER_SHIFT = 8;  
  30.   
  31.     abstract double overtimePay(double hrs, double payRate);  
  32.   
  33.     double pay(double hoursWorked, double payRate) {  
  34.       double basePay = hoursWorked * payRate;  
  35.       return basePay + overtimePay(hoursWorked, payRate);  
  36.     }  
  37.   }  
  38. }  

测试

[java]  view plain  copy
  1. System.out.println("时薪100的人在周五工作8小时的收入:" + PayrollDay.FRIDAY.pay(8.0100));  
  2. System.out.println("时薪100的人在周六工作8小时的收入:" + PayrollDay.SATURDAY.pay(8.0100));  

EnumSet和EnumMap

Java中提供了两个方便操作enum的工具类——EnumSet和EnumMap。

EnumSet 是枚举类型的高性能Set实现。它要求放入它的枚举常量必须属于同一枚举类型。

EnumMap 是专门为枚举类型量身定做的Map实现。虽然使用其它的Map实现(如HashMap)也能完成枚举类型实例到值得映射,但是使用EnumMap会更加高效:它只能接收同一枚举类型的实例作为键值,并且由于枚举类型实例的数量相对固定并且有限,所以EnumMap使用数组来存放与枚举类型对应的值。这使得EnumMap的效率非常高。

[java]  view plain  copy
  1. // EnumSet的使用  
  2. System.out.println("EnumSet展示");  
  3. EnumSet<ErrorCodeEn> errSet = EnumSet.allOf(ErrorCodeEn.class);  
  4. for (ErrorCodeEn e : errSet) {  
  5.   System.out.println(e.name() + " : " + e.ordinal());  
  6. }  
  7.   
  8. // EnumMap的使用  
  9. System.out.println("EnumMap展示");  
  10. EnumMap<StateMachine.Signal, String> errMap = new EnumMap(StateMachine.Signal.class);  
  11. errMap.put(StateMachine.Signal.RED, "红灯");  
  12. errMap.put(StateMachine.Signal.YELLOW, "黄灯");  
  13. errMap.put(StateMachine.Signal.GREEN, "绿灯");  
  14. for (Iterator<Map.Entry<StateMachine.Signal, String>> iter = errMap.entrySet().iterator(); iter.hasNext();) {  
  15.   Map.Entry<StateMachine.Signal, String> entry = iter.next();  
  16.   System.out.println(entry.getKey().name() + " : " + entry.getValue());  

猜你喜欢

转载自blog.csdn.net/justlpf/article/details/80756581