JDK1.6增添了一些新的特性,其中枚举就是其中比较重要的一种最新的特性。下面这篇文章来详细谈谈java枚举类的原理和应用。
先用普通的java类来模拟一下枚举类。在做web应用的时候经常需要进行权限控制,比如一个系统有“普通会员”,“管理员”,“超级管理员”这三 个权限,这个系统是由一组开发人员共同来设计,一些新晋的程序员可能并不知道这三个权限的具体名称,为了省去程序员之间相互询问的麻烦,可以很好的用下面 的设计来确保每位程序员只能使用事先定义好的三种权限,否则将通不过编绎器的法眼。
class="dp-j">
- package??net.csdn.blog;??
- ??
- public???abstract???class??AccessControll?{??
- ??
- ????public???static???final??AccessControll?MEMBER?=??new??AccessControll()?{??
- ????????public??String?toString()?{??
- ????????????return???"普通会员"?;??
- ????????}??
- ????};??
- ????public???static???final??AccessControll?ADMIN?=??new??AccessControll()?{??
- ????????public??String?toString()?{??
- ????????????return???"管理员"?;??
- ????????}??
- ??
- ????};??
- ????public???static???final??AccessControll?SUPERADMIN?=??new??AccessControll()?{??
- ????????public??String?toString()?{??
- ????????????return???"超级管理员"?;??
- ????????}??
- ????};??
- ??
- ????public???abstract??String?toString();??
- ??
- }??
?
Java代码?
- package?net.csdn.blog;??
- public?abstract?class?AccessControll?{??
- ????public?static?final?AccessControll?MEMBER?=?new?AccessControll()?{??
- ????????public?String?toString()?{??
- ????????????return?"普通会员";??
- ????????}??
- ????};??
- ????public?static?final?AccessControll?ADMIN?=?new?AccessControll()?{??
- ????????public?String?toString()?{??
- ????????????return?"管理员";??
- ????????}??
- ????};??
- ????public?static?final?AccessControll?SUPERADMIN?=?new?AccessControll()?{??
- ????????public?String?toString()?{??
- ????????????return?"超级管理员";??
- ????????}??
- ????};??
- ????public?abstract?String?toString();??
- }??
?
在外部的程序员如果需要选择用户的权限时可以按照下面的方法做
- package??net.csdn.blog;??
- ??
- public???class??EnumTest?{??
- ??
- ??
- ????public???static???void??main(String[]?args)?{??
- ????????AccessControll?memeber=AccessControll.MEMBER;??
- ????????AccessControll?admin=AccessControll.ADMIN;??
- ????????AccessControll?superadmin=AccessControll.SUPERADMIN;??
- ????????System.out.println(memeber);??
- ????????System.out.println(admin);??
- ????????System.out.println(superadmin);??
- ??
- ????}??
- ??
- }??
?
Java代码?
- package?net.csdn.blog;??
- public?class?EnumTest?{??
- ????public?static?void?main(String[]?args)?{??
- ????????AccessControll?memeber=AccessControll.MEMBER;??
- ????????AccessControll?admin=AccessControll.ADMIN;??
- ????????AccessControll?superadmin=AccessControll.SUPERADMIN;??
- ????????System.out.println(memeber);??
- ????????System.out.println(admin);??
- ????????System.out.println(superadmin);??
- ????}??
- }??
?
自从有了java枚举类之后,就不再需要我们自己去写一个诸如AccessControll这样的类了,可以直接用枚举类来进行权限范围的控制,如EnumAccessControll.java所示
- package??net.csdn.blog;??
- ??
- public???enum??EnumAccessControll?{??
- ??
- ????MEMBER("普通会员"?),ADMIN(?"管理员"?),SUPERADMIN(?"超级管理员"?);????
- ??????
- ????private??String?name;??
- ??????
- ????private??EnumAccessControll(String?name){??
- ????????this?.name=name;??
- ????}??
- ????public??String?toString(){??
- ????????return??name;??
- ????}??
- ??????
- }??
?
Java代码?
- package?net.csdn.blog;??
- public?enum?EnumAccessControll?{??
- ????MEMBER("普通会员"),ADMIN("管理员"),SUPERADMIN("超级管理员");??
- ??????
- ????private?String?name;??
- ??????
- ????private?EnumAccessControll(String?name){??
- ????????this.name=name;??
- ????}??
- ????public?String?toString(){??
- ????????return?name;??
- ????}??
- ??????
- }??
?
使用枚举类有几个要注意得地方:
- 枚举元素列表必须写在枚举类的最前面,每个元素之间用逗号隔开,元素列表结束位置后若没有其他内容,则可以不写分号,否则必须要写。
- 枚举类中所有的构造器是private的,外部程序不能够创建枚举类的实例。枚举类中可以显式的指明调用哪个构建器,如MEMBER和 MEMBER()这两种元素列表声明是等价的,都是调用默认的构建器,而MEMBER("普通会员")则是显式的指明程序调用第二个构建器.
- 枚举类可以有抽象方法,但必须在元素列表声明中实现这些方法。此外,枚举类中还可以声明一些普通的成员变量和方法,如上例所示。
枚举类中常见方法
- toString()方法,显示枚举类元素,上例中对这个方法进行了覆盖,如果不覆盖它的话一般打印变量名,上例中为"MEMBER","ADMIN","SUPERADMIN";
- valueOf(String arg0)方法,通过传入的arg0字符串解析出一个该枚举类的实例,其中传入的字符串必须是元素列表的名称之一,否则将抛出 java.lang.IllegalArgumentException异常,在web应用中这个方法作用很重要。该方法为静态方法,不需创建实例即可使 用,如通过EnumAccessControll.valueOf("MEMBER")即可返回Member实例.
- values()方法,返回一个该枚举类的数组,其中数组的元素即为该枚举类中元素列表中的元素。
- ordinal()方法,返回枚举元素实例中元素列表中的位置,起始位置为0。如EnumAccessControll.SUPERADMIN.ordinal()的结果为2。
- compareTo()方法,比较两个元素。
- name()方法,默认情况下返回元素实列的变量名,该方法为final不可覆盖的.
枚举类的“不同寻常之处”
枚举类可以作为在switch语句中作为分支结构的条件使用,如下例所示。
- package??net.csdn.blog;??
- ??
- public???class??SwitchEnum?{??
- ??
- ????public???static???void??main(String?args[])?{??
- ????????sysEnum(EnumAccessControll.SUPERADMIN);??
- ????}??
- ??
- ????public???static???void??sysEnum(EnumAccessControll?eac)?{??
- ????????switch??(eac)?{??
- ????????case??MEMBER:??
- ????????????System.out.println("我的权限为普通员工"?);??
- ????????????break?;??
- ????????case??ADMIN:??
- ????????????System.out.println("我是管理员"?);??
- ????????????break?;??
- ????????case??SUPERADMIN:??
- ????????????System.out.println("我是超级管理员"?);??
- ????????????break?;??
- ????????}??
- ??
- ????}??
- }??
?
Java代码?
- package?net.csdn.blog;??
- public?class?SwitchEnum?{??
- ????public?static?void?main(String?args[])?{??
- ????????sysEnum(EnumAccessControll.SUPERADMIN);??
- ????}??
- ????public?static?void?sysEnum(EnumAccessControll?eac)?{??
- ????????switch?(eac)?{??
- ????????case?MEMBER:??
- ????????????System.out.println("我的权限为普通员工");??
- ????????????break;??
- ????????case?ADMIN:??
- ????????????System.out.println("我是管理员");??
- ????????????break;??
- ????????case?SUPERADMIN:??
- ????????????System.out.println("我是超级管理员");??
- ????????????break;??
- ????????}??
- ????}??
- }??
?
足够复杂的一个枚举类HardEnum.java
?
- package??net.csdn.blog;??
- ??
- public???enum??HardEnum?{??
- ????RED("红灯"?)?{??
- ????????public??HardEnum?nextLamp()?{??
- ????????????return??HardEnum.valueOf(?"YELLOW"?);??
- ????????}??
- ????},??
- ????YELLOW("黄灯"?)?{??
- ????????public??HardEnum?nextLamp()?{??
- ????????????return??HardEnum.valueOf(?"GREEN"?);??
- ????????}??
- ????},??
- ????GREEN("绿灯"?)?{??
- ????????public??HardEnum?nextLamp()?{??
- ????????????return??HardEnum.valueOf(?"RED"?);??
- ????????}??
- ????},??
- ????UNKNOW?{??
- ????????public??HardEnum?nextLamp()?{??
- ????????????if??(?this?.ordinal()?+??1??!=??this?.values().length)??
- ????????????????return???this?.values()[?this?.ordinal()?+??1?];??
- ????????????else???
- ????????????????return???this?.values()[?0?];??
- ????????}??
- ????};??
- ????private??String?name;??
- ??
- ????public???abstract??HardEnum?nextLamp();??
- ??
- ????private??HardEnum()?{??
- ????????name?=?"未知"?;??
- ????};??
- ??
- ????private??HardEnum(String?n)?{??
- ????????name?=?n;??
- ????}??
- ??
- }??
?
Java代码?
- package?net.csdn.blog;??
- public?enum?HardEnum?{??
- ????RED("红灯")?{??
- ????????public?HardEnum?nextLamp()?{??
- ????????????return?HardEnum.valueOf("YELLOW");??
- ????????}??
- ????},??
- ????YELLOW("黄灯")?{??
- ????????public?HardEnum?nextLamp()?{??
- ????????????return?HardEnum.valueOf("GREEN");??
- ????????}??
- ????},??
- ????GREEN("绿灯")?{??
- ????????public?HardEnum?nextLamp()?{??
- ????????????return?HardEnum.valueOf("RED");??
- ????????}??
- ????},??
- ????UNKNOW?{??
- ????????public?HardEnum?nextLamp()?{??
- ????????????if?(this.ordinal()?+?1?!=?this.values().length)??
- ????????????????return?this.values()[this.ordinal()?+?1];??
- ????????????else??
- ????????????????return?this.values()[0];??
- ????????}??
- ????};??
- ????private?String?name;??
- ????public?abstract?HardEnum?nextLamp();??
- ????private?HardEnum()?{??
- ????????name?=?"未知";??
- ????};??
- ????private?HardEnum(String?n)?{??
- ????????name?=?n;??
- ????}??
- }??
?
?
由此,java枚举类的特性就介绍完了,相信大家也对java的枚举类有了一定的了解了.
?
?
转自:http://tianhandigeng.iteye.com/blog/1094867