定义枚举
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枚举的使用
时间: 2024-10-16 07:01:12