码迷,mamicode.com
首页 > 编程语言 > 详细

枚举Enum

时间:2018-03-09 18:57:56      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:person   lin   blog   stat   rect   value   def   rate   宝马车   

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

标签:person   lin   blog   stat   rect   value   def   rate   宝马车   

原文地址:https://www.cnblogs.com/qqyong123/p/8535308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!