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

java枚举的使用

时间:2014-07-31 21:03:31      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:java   使用   io   ar   text   return   c   public   

定义枚举

public enum YesOrNo {
	YES("是") , 
	
	NO("否") ;  
	
	private String text ; 
	
	private int code ;
	
	YesOrNo(){
		this.text = this.name() ; 
		this.code = this.ordinal() ; 
	}
	
	YesOrNo(String text){
		this.text = text ; 
		this.code = this.ordinal() ; 
	}
	
	YesOrNo(String text,int code){
		this.text = text ; 
		this.code = code ; 
	}
	
	public String getText() {
		return text;
	}
	
	public int getCode() {
		return code;
	}
}

调用枚举:

public static void main(String[] args) throws Exception{
		Method method = MethodUtils.getAccessibleMethod(YesOrNo.class  , "getText") ;  
		Method method1 = MethodUtils.getAccessibleMethod(YesOrNo.class  , "getCode") ; // 使用类反射
		
		System.out.println( method.invoke( YesOrNo.YES ) ) ;
		System.out.println( method1.invoke( YesOrNo.NO ) ) ; 
		
		System.out.println( YesOrNo.YES.getText() );
	}


java枚举的使用,布布扣,bubuko.com

java枚举的使用

标签:java   使用   io   ar   text   return   c   public   

原文地址:http://blog.csdn.net/hfmbook/article/details/38320493

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