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()); } }