首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。