大家好,又见面了,我是全栈君。
定义枚举
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() );
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116585.html原文链接:https://javaforall.cn