java枚举的使用

定义枚举

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() );
	}


posted @ 2015-12-22 18:29  yxwkaifa  阅读(188)  评论(0编辑  收藏  举报