Java中的Enum(枚举)用法介绍

1. 关于Java Enum: 学过C/C++等语言的人,应该都对Enum类型略知一二。Enum一般用来表示一组相同类型的常量。如性别、日期、月份、颜色等。对这些属性用常量的好处是显而易见的,不仅可以保证单例,且比较时候可以用”==”来替换equals。是一种好的习惯。JDK1.5之前没有Enum这个类型,那时候一般用接口常量来替代。有了JavaEnum之后,可以更贴近的表示这种常量。

2. 如何使用Java Enum 简单的用法:JavaEnum简单的用法一般用于代表一组常用常量,可用来代表一类相同类型的常量值。如:

性别: public enum SexEnum {



male, female;



}



颜色: public enum Color {



RED, BLUE,GREEN,BLACK;



}

  

枚举对象里面的值都必须是唯一的。

可以通过Enum类型名直接引用该常量,如SexEnum.male,Color.RED.

复杂用法:Java为枚举类型提供了一些内置的方法,同事枚举常量还可以有自己的方法。可以很方便的遍历枚举对象,看个下面的例子:

1. 代码一 WeekDay.java:

public enum WeekDay {



Mon("Monday"), Tue("Tuesday"), Wed("Wednesday"), Thu("Thursday"), Fri(



"Friday"), Sat("Saturday"), Sun("Sunday");



private final String day;



private WeekDay(String day) {



this.day = day;



}



public static void printDay(int i){



switch(i){



case 1: System.out.println(WeekDay.Mon); break;



case 2: System.out.println(WeekDay.Tue);break;



case 3: System.out.println(WeekDay.Wed);break;



case 4: System.out.println(WeekDay.Thu);break;



case 5: System.out.println(WeekDay.Fri);break;



case 6: System.out.println(WeekDay.Sat);break;



case 7: System.out.println(WeekDay.Sun);break;



default:System.out.println("wrong number!");



}



}



public String getDay() {



return day; } }



2. 代码二 WeekDayTest.java:



public class WeekDayTest {



public static void main(String args[]) {



for (WeekDay day : WeekDay.values()) {



System.out.println(day + "====>" + day.getDay()); } WeekDay.printDay(5);



}



}



输出结果为:



Mon====>Monday



Tue====>Tuesday



Wed====>Wednesday



Thu====>Thursday



Fri====>Friday



Sat====>Saturday



Sun====>Sunday



Fri

  

3. Java Enum原理

Java Enum类型的语法结构尽管和java类的语法不一样,应该说差别比较大。但是经过编译器编译之后产生的是一个class文件。该class文件经过反编译可以看到实际上是生成了一个类,该类继承了java.lang.Enum<E>.

WeekDay经过反编译(javap WeekDay命令)之后得到的内容如下(去掉了汇编代码):

public final class WeekDay extends java.lang.Enum{



public static final WeekDay Mon;



public static final WeekDay Tue;



public static final WeekDay Wed;



public static final WeekDay Thu;



public static final WeekDay Fri;



public static final WeekDay Sat;



public static final WeekDay Sun;



static {};



public static void printDay(int);



public java.lang.String getDay();



public static WeekDay[] values();



public static WeekDay valueOf(java.lang.String);

}

  

所以实际上Enum类型就是以Java类来实现的,没有什么新的特点,只不过java编译器帮我们做了语法的解析和编译。完全也可以自己实现。但是既然有这样方便一个东西,当然会去用了。

 

使用实例:

1》:以Class定义的

public class PCUEnum { public static void main(String[] args) { 



System.out .println(PCUEnum.TaskStatus.get(PCUEnum.TaskStatus.DELETED));



 }



public enum TaskStatus { /



/ 1:等待执行 2:正在执行 3:执行完毕 4:执行失败 5:暂停 100:已经删除 101:已经送审 NOTINQUEUE,WAITING, EXECUTING, EXECUTED, FAILED, SUSPEND, DELETED,WAITING_AUDIT;



public static int get(TaskStatus s) { 



int result = 0; switch (s) { case NOTINQUEUE: result=0; break; case WAITING: result = 1; break; case EXECUTING: result = 2; break; case EXECUTED: result = 3; break; case FAILED: result = 4; break; case SUSPEND: result = 5; break; case DELETED: result = 100; break; case WAITING_AUDIT: result = 101; break; } return result; 



} 



   }



     }

  

1.1》:以类为实例:

public class EnumClass {



enum explore { HERE, THERE } enum WeekDay { Mon("Monday"), Tue("Tuesday"), Wed("Wednesday"), Thu("Thursday"), Fri("Friday"), Sat("Saturday"), Sun("Sunday"), Wro("Wrong day");



private String day;



WeekDay(String day){ this.day = day; }



public String getDay() { return day; } } public static void main(String[] args) { System.out.println(EnumClass.explore.HERE); System.out.println("--------"); for(EnumClass.WeekDay day : EnumClass.WeekDay.values()) { System.out.println(day.getDay()); 

            }

         }

 }

  

 

2》:以Enum定义的

package com.zhou.test;



public enum TaskEnumStatus {



 NOTINQUEUE(1),WAITING(2), EXECUTING(3), EXECUTED(4), FAILED(5), SUSPEND(6), DELETED(7),WAITING_AUDIT(8);



   private int i;



   private TaskEnumStatus(int i) {



     this.i = i;



   }



   public int getEnumStatus() {



     return i;



   }



}





public static void main(String[] args) {



     



    System.out.println(TaskEnumStatus.NOTINQUEUE + "=" + TaskEnumStatus.NOTINQUEUE.getEnumStatus());



     



  }

  

2.1》:以Enum定义

 

public enum UserStatusEnum {



 DISABLE("disable"), ENABLE("enable"); private String value;

 UserStatusEnum(String value){ this.value = value;



 }

 public String getValue() {



 return value; 



} 



public String toString() {



 return value; 



}



 public static void main(String[] args) { 



System.out.println(UserStatusEnum.DISABLE);



 }   



}

 

  


2.2》:以Enum定义

 

public enum NumberEnum { 



CONST_FOUR(4), CONST_ENIGHT(8), CONST_NINE(999999); private final int value;

 NumberEnum(int value) { 



this.value = value; 



}

 public int getValue() { 



return value; 



}

 public static void main(String[] args) {



   System.out.println(NumberEnum.CONST_ENIGHT.getValue());



 } 



} 

原文地址:http://blog.sina.com.cn/s/blog_52fea7b60100phjl.html

如需转载请注明出处

  

 

 

 

你可能感兴趣的:(java)