publicenumEnumTest {
FRANK("The given name of me"),
LIU("The family name of me");
privateString context;
privateString getContext(){
returnthis.context;
}
privateEnumTest(String context){
this.context = context;
}
publicstaticvoidmain(String[] args){
for(EnumTest name :EnumTest.values()){
System.out.println(name+" : "+name.getContext());
}
System.out.println(EnumTest.FRANK.getDeclaringClass());
}
}
Java中枚举实现的
综合:
示例:
publicenumColor{
RED,BLUE,BLACK,YELLOW,GREEN
}
显然,enum很像特别的class,实际上enum申明定义的类型便是一个类 。 而这些类都是类库中Enum类的子类(java.lang.Enum) 。它们继承了这个Enum中的许多有用的
步骤 。我们对代码编译之后发现,编译器将enum类型
径自编译成了一个字节码文件:Color.class 。
Color字节码代码:
finalenumhr.test.Color {
// 所有的枚举值都是类静态常量
publicstaticfinalenumhr.test.Color RED;
publicstaticfinalenumhr.test.Color BLUE;
publicstaticfinalenumhr.test.Color BLACK;
publicstaticfinalenumhr.test.Color YELLOW;
publicstaticfinalenumhr.test.Color GREEN;
privatestaticfinalsynthetic hr.test.Color[] ENUM$VALUES;
// 初始化过程,对枚举类的所有枚举值对象进行第一次初始化
static{
0newhr.test.Color [1]
3dup
4ldc "RED"> [16]//把枚举值字符串"RED"压入操作数栈
6// 把整型值0压入操作数栈
7invokespecial hr.test.Color(java.lang.String,int) [17]//调用Color类的私有
构造器
缔造Color对象RED
10putstatic hr.test.Color.RED : hr.test.Color [21]//将枚举对象赋给Color的静态常量RED 。
......... 枚举对象BLUE等与上同
102return
};
// 私有
构造器,外部不可能动态
缔造一个枚举类对象(也便是不可能动态
缔造一个枚举值) 。
privateColor(java.lang.String arg0,intarg1){
// 调用父类Enum的受
掩护
构造器
缔造一个枚举对象
3invokespecial java.lang.Enum(java.lang.String,int) [38]
};
publicstatichr.test.Color[] values();
// 实现Enum类的
形象
步骤
publicstatichr.test.Color valueOf(java.lang.String arg0);
}
下面我们就
详尽介绍enum定义的枚举类的
特色及其用法 。(后面均用Color举例)
1、Color枚举类便是class,并且是一个不
可以被继承的final类 。
其枚举值(RED,BLUE...)都是Color类型的类静态常量, 我们
可以通过下面的
模式来得到Color枚举类的一个实例:
Color c=Color.RED;
留神:这些枚举值都是public static final的,也便是我们
时常所定义的常量
模式,
因此枚举类中的枚举值最好所有大写 。
2、即然枚举类是class,固然在枚举类型中有
构造器,
步骤和数据域 。然而,枚举类的
构造器有很大的不同:
(1)
构造器只不过在
构造枚举值的时候被调用 。
enumColor{
RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
//
构造枚举值,
比方RED(255,0,0)
privateColor(intrv,intgv,intbv){
this.redValue=rv;
this.greenValue=gv;
this.blueValue=bv;
}
publicString toString(){//
遮蔽了父类Enum的toString()
returnsuper.toString()+"("+redValue+","+greenValue+","+blueValue+")";
}
privateintredValue;//自定义数据域,private为了封装 。
privateintgreenValue;
privateintblueValue;
}
(2)
构造器不得不私有private,绝对不同意有public
构造器 。 这样
可以
保障外部代码
无奈新
构造枚举类的实例 。这也是
彻底
相符情理的,由于我们晓得枚举值是public static final的常量而已 。
但枚举类的
步骤和数据域
可以同意外部
拜访 。
publicstaticvoidmain(String args[])
{
// Color colors=new Color(100,200,300); //wrong
Color color=Color.RED;
System.out.println(color);// 调用了toString()
步骤
}
3、所有枚举类都继承了Enum的
步骤,下面我们
详尽介绍这些
步骤 。
(1) ordinal()
步骤: 返回枚举值在枚举类种的顺序 。这个顺序依据枚举值申明的顺序而定 。
Color.RED.ordinal();//返回
后果:0
Color.BLUE.ordinal();//返回
后果:1
(2) compareTo()
步骤: Enum实现了java.lang.Comparable接口,
因此
可以
比较象与指定对象的顺序 。Enum中的compareTo返回的是两个枚举值的顺序之差 。固然,前提是两个枚举值必须属于同一个枚举类,不然会抛出ClassCastException()
异样 。(具体可见源代码)
Color.RED.compareTo(Color.BLUE);//返回
后果 -1
(3) values()
步骤: 静态
步骤,返回一个包括所有枚举值的数组 。
Color[] colors=Color.values();for(Color c:colors){ System.out.print(c+","); }
//返回
后果:RED,BLUE,BLACK YELLOW,GREEN,
(4) toString()
步骤: 返回枚举常量的名称 。
Color c=Color.RED;
System.out.println(c);//返回
后果: RED
(5) valueOf()
步骤: 这个
步骤和toString
步骤是
绝对应的,返回带指定名称的指定枚举类型的枚举常量 。
Color.valueOf("BLUE");//返回
后果: Color.BLUE
(6) equals()
步骤:
比较两个枚举类对象的
引用 。
//JDK源代码:
publicfinalbooleanequals(Object other) {
returnthis==other;
}
4、枚举类
可以在switch语句中
使用 。
Color color=Color.RED;
switch(color){
caseRED: System.out.println("its red");break;
caseBLUE: System.out.println("its blue");break;
caseBLACK: System.out.println("its blue");break;
}
原文链接:http://www.cnblogs.com/frankliiu-java/archive/2010/12/07/1898721.html