首页 > 代码库 > java enum

java enum

java枚举类型

java枚举类型类似java中的普通类,编译后生成.class字节码文件。

参考:http://www.cnblogs.com/frankliiu-java/archive/2010/12/07/1898721.html

http://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html

直接使用关键字enum enum_type_name就行了

Color.java

 1 package com.gxf.enumTest; 2  3 enum Color { 4     RED(255, 0, 0),GREEN(0,255,0),BLUE(0,0,255); 5     private int valueRed; 6     private int valueGreen; 7     private int valueBlue; 8      9     Color(int red, int green, int blue){10         this.valueRed = red;11         this.valueGreen = green;12         this.valueBlue = blue;13     }14     public String toString(){15         return "(valueRed = " + this.valueRed + "," + "valueGreen = " + valueGreen + "," 16                 + "valueBlue = " + valueBlue + ")";17     }18 }19 enum Fruit{20     APPLE, ORANGE, BANANA;21 }

test.java

 1 package com.gxf.enumTest; 2  3 public class test { 4  5     public static void main(String[] args) { 6         Color redColor = Color.RED; 7         System.out.println(redColor.toString()); 8          9         Fruit apple = Fruit.APPLE;10         System.out.println(apple);11         System.out.println(apple.ordinal());12     }13 14 }

技术分享

java enum