枚举类型是JDK5.0的新特征。Java enum 在像C这样强调数据结构的语言里,枚举是必不可少的一种数据类型。然而在java的早期版本中,是没有一种叫做enum的独立数据结构的。所以在以前的java版本中,我们经常使用interface来simulate一个enum。
- public interface Color {
- static int RED = 1;
- static int GREEN = 2;
- static int BLUE = 3;
- }
虽然这种simulation比较麻烦,但在以前也还应付的过去。可是随着java语言的发展,越来越多的呼声要求把enum这种数据结构独立出来,加入到java中。所以从java 1.5以后,就有了enum,这也是这篇blog要学习的topic。
学习的最好方式就是例子,先来一个:
- public class EnumDemo {
- private enum Color {red, blue, green}//there is not a ";"
- public static void main(String[] args) {
- for(Color s : Color.values()) {
- //enum的values()返回一个数组,这里就是Seasons[]
- System.out.println(s);
- }
- }
- }
结果如下:
- red
- blue
- green
注意事项已经在code中注释出,还要说明一点的是,这个java文件编译完成后不只有一个EnumDemo.class,还会有一个EnumDemo$Seasons.class,奇怪吧!
显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类。而这些类都是类库中Enum类的子类(java.lang.Enum<E>)。它们继承了这个Enum中的许多有用的方法。下面我们就详细介绍enum定义的枚举类的特征及其用法。
- public class EnumDemo {
- private enum Color {red, blue, green}//there is not a ";"
- public static void main(String[] args) {
- Color s = Color.blue;
- switch (s) {
- case red://notice: Seasons.red will lead to compile error
- System.out.println("red case");
- break;
- case blue:
- System.out.println("blue case");
- break;
- case green:
- System.out.println("green case");
- break;
- default:
- break;
- }
- }
- }
就这么多吗,当然不是,我们的enum结构还可以定义自己的方法和属性。
- public class EnumDemo {
- private enum Color {
- red, blue, green;//there is a ";"
- //notic: enum's method should be "static"
- public static Color getColor(String s){
- if(s.equals("red flag")){
- return red;
- } else if(s.equals("blue flag")){
- return blue;
- } else {
- return green;
- }
- }
- }//there is not ";"
- public static void main(String[] args) {
- EnumDemo demo = new EnumDemo();
- System.out.println(demo.getFlagColor("red flag"));
- }
- public Color getFlagColor(String string){
- return Color.getColor(string);
- }
- }
public enum Color{
RED,BLUE,BLACK,YELLOW,GREEN
}
1、Color枚举类是特殊的class,其枚举值(RED,BLUE...)是Color的类对象(类实例):Color c=Color.RED;
而且这些枚举值都是public static final的,也就是我们经常所定义的常量方式,因此枚举类中的枚举值最好全部大写。
2、即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。但是,枚举类的构造器有很大的不同:
(1) 构造器只是在构造枚举值的时候被调用。
- enum Color{
- RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
- //构造枚举值,比如RED(255,0,0)
- private Color(int rv,int gv,int bv){
- this.redValue=rv;
- this.greenValue=gv;
- this.blueValue=bv;
- }
- public String toString(){ //自定义的public方法
- return super.toString()+"("+redValue+","+greenValue+","+blueValue+")";
- }
- private int redValue; //自定义数据域,private为了封装。
- private int greenValue;
- private int blueValue;
- }
(2) 构造器只能私有private,绝对不允许有public构造器。这样可以保证外部代码无法新构造枚举类的实例。这也是完全符合情理的,因为我们知道枚举值是public static final的常量而已。 但枚举类的方法和数据域可以允许外部访问。
- public static void main(String args[])
- {
- // Color colors=new Color(100,200,300); //wrong
- Color color=Color.RED;
- System.out.println(color); // 调用了toString()方法
- }
(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源代码:
- public final boolean equals(Object other) {
- return this==other;
- }