首页 > 编程语言 > 详细

java枚举的使用

时间:2015-12-22 19:30:18      阅读:188      评论:0      收藏:0      [点我收藏+]

定义枚举

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枚举的使用

原文:http://www.cnblogs.com/yxwkf/p/5067730.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!