http://blog.csdn.net/qq_27093465/article/details/52180865
https://www.cnblogs.com/hyl8218/p/5088287.html
枚举其实也可以看作是一个对象类,它里面也是有属性、构造方法的、方法体的;
package enumTest; enum Car { 现代,奔驰,宝马,大众 } public class EnumTest { public static void main(String[] args) { Car car = Car.奔驰; switch(car){ case 现代 : System.out.println("现代车"); break; case 奔驰 : System.out.println("奔驰车"); break; case 宝马 : System.out.println("宝马车"); break; case 大众 : System.out.println("大众车"); break; default : System.out.println("不知道是什么车"); break; } } }
package enumTest; public enum EnumTest01 { PERSONAL_AGENT("1", "aaa"), PART_AGENT("2", "ssss"), OFFICAL_AGENT("3", "cccc"), TRADITION_AGENT("4", "经济学"), COMPERATE_AGENT("6", "sxsx"), DIRECT_BUSINESS("D", "心理学"), XUEKE_LINCHAUNG("5","临床学"); private String key; private String desc; private EnumTest01(String key, String desc) { this.key = key; this.desc = desc; } public static EnumTest01 getByKey(String key) { for (EnumTest01 bun : EnumTest01.values()) { if (bun.key.equals(key)) { return bun; } } throw new IllegalArgumentException("No element matches " + key); } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public static void main(String[] args) { EnumTest01 bun = EnumTest01.getByKey("5"); System.out.println("bun:" + bun.getDesc()); } }
枚举类型的调用
/** * 调用enum方法,直接调用就可以 * */ public class EnumDiaoYongTest { public static void main(String[] args) { //EnumTest xueke = new EnumTest(); EnumTest xueke = EnumTest.getByKey("D"); System.out.println(xueke.getDesc()); } }