在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的。
例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素组成了四季的“数据集”。
在java中如何更好的使用这些“数据集”呢?在jdk1.5之前我们或许会这样写:
static class Grade { private Grade() { } public static final Grade A = new Grade(); public static final Grade B = new Grade(); public static final Grade C = new Grade(); public static final Grade D = new Grade(); public static final Grade E = new Grade(); }
@Test public void test() { print(Grade.A); } private void print(Grade grade) { System.out.println(grade.toString()); } //定义一个枚举类,里面每一个枚举值就代表一个对象 enum Grade{ A,B,C,D,E; }
@Test public void test() { print(Grade.A); } private void print(Grade grade) { System.out.println(grade.label); System.out.println(grade.getLabel()); } enum Grade { A("100-91"), B("90-81"), C("80-71"), D("70-60"), E("60-0"); private String label; private Grade(String label) { this.label = label; } private String getLabel(){ return this.label; } }
@Test public void test() { print(Grade.B); } private void print(Grade grade) { System.out.println(grade.label); System.out.println(grade.getLabel()); System.out.println(grade.getCNLebal()); } enum Grade { A("100-91") { @Override public String getCNLebal() { return "优秀"; } }, B("90-81") { @Override public String getCNLebal() { return "良好"; } }, C("80-71") { @Override public String getCNLebal() { return "一般"; } }, D("70-60") { @Override public String getCNLebal() { return "及格"; } }, E("60-0") { @Override public String getCNLebal() { return "差"; } }; private String label; private Grade(String label) { this.label = label; } private String getLabel() { return this.label; } public abstract String getCNLebal(); }
@Test public void test() { print(Grade.B); // Grade.values() 得到所有的枚举值 ,返回一个数组 System.out.println("分数等级个数为:" + Grade.values().length); for (Grade grade : Grade.values()) { String name = grade.name(); System.out.println("当枚举值的name为:" + name); int ordinal = grade.ordinal(); System.out.println("当前枚举值的坐标为:" + ordinal); } //Grade.valueOf("W") Grade.valueOf(Grade.class, "E") 将当前字符串转换成一个枚举,如果失败则抛出参数非法异常 Grade grade = Grade.valueOf("C"); print(grade); Grade grade2 = Grade.valueOf(Grade.class, "E"); print(grade2); } private void print(Grade grade) { System.out.println(grade.label); System.out.println(grade.getLabel()); System.out.println(grade.getCNLebal()); } enum Grade { A("100-91") { @Override public String getCNLebal() { return "优秀"; } }, B("90-81") { @Override public String getCNLebal() { return "良好"; } }, C("80-71") { @Override public String getCNLebal() { return "一般"; } }, D("70-60") { @Override public String getCNLebal() { return "及格"; } }, E("60-0") { @Override public String getCNLebal() { return "差"; } }; private String label; private Grade(String label) { this.label = label; } private String getLabel() { return this.label; } public abstract String getCNLebal(); }
public enum ServerCode { UNKNOWN(-1, "未知错误"), SUCCESS(0, "成功"), PARSE_ERROR(100,"返回值解析出错!"), NET_IO_ERROR(101,"网络通讯出错!"); private int code = 0; private String message = ""; private ServerCode(int errorCode, String message) { this.code = errorCode; this.message = message; } public static ServerCode valueOf(int errorCode) { for (ServerCode code : values()) { if (code.code == errorCode) { return code; } } return UNKNOWN; } public int code() { return code; } @Override public String toString() { return message; }
常用用法说完了,大体总结一下吧:
原文地址:http://blog.csdn.net/u010358168/article/details/46520687