码迷,mamicode.com
首页 > 编程语言 > 详细

java Enum 用法示例

时间:2016-07-29 20:59:19      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

public enum MyEnum {

	Monday,
	Tuesday,
	Wednesday,
	Thursady,
	Friday,
	Saturday,
	Sunday;
	public static void main(String[]args){
		//Enum 对象
		MyEnum mye;
		mye=MyEnum.Sunday;
		MyEnum mye1=MyEnum.Monday;
		/**
		 * enum convert to int
		 *  int java.lang.Enum.ordinal()
		 */
		System.out.println(mye.ordinal());
		System.out.println(mye1.ordinal());
		/**
		 * toString()
		 * String java.lang.Enum.name()
		 */
		System.out.println(mye);
		System.out.println(mye.name());
		/**
		 * int convert to enum
		 */
		MyEnum mye2=MyEnum.values()[1];
		MyEnum mye3=mye2.values()[2];
		System.out.println(mye2);
		System.out.println(mye3);
		/**
		 * is equal or not
		 * boolean java.lang.Enum.equals(Object other)
		 */		
		System.out.println(MyEnum.Monday.equals(MyEnum.Monday));
		/**
		 * foreach out
		 */
		System.out.println(mye.values().length);
		for (MyEnum e : MyEnum.values()) {
			System.out.println(e);
		}	
	}
}

  运行结果:

6
0
Sunday
Sunday
Tuesday
Wednesday
true
7
Monday
Tuesday
Wednesday
Thursady
Friday
Saturday
Sunday

技术分享

java Enum 用法示例

标签:

原文地址:http://www.cnblogs.com/PleaseRemember/p/5719394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!