枚举类中Enum的values方法
枚举可以说是在我们平常开发中用的很多了,它里面有一个values方法算是比较特殊的。今天特意简单的说一下它的使用。
public enum TestEnum { A(0,"正常"), B(1,"异常"), C(2,"有点不正常"); private Integer code; private String msg; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } TestEnum(Integer code, String msg) { this.code = code; this.msg = msg; } public static void main(String[] args) { System.out.println("枚举值为:"+Arrays.toString(TestEnum.values())); for (TestEnum testEnum : TestEnum.values()) { System.out.println("code值为:"+testEnum.code); System.out.println("msg值为:"+testEnum.msg); } } }
测试结果: