java-枚举示例

package cd.itcast.day1;

/**
 * 声明了一个枚举类型,这个类型里面只有Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,
 * Sunday几个实例
 * 
 * @author Administrator
 * 
 */
public enum WeekDays { 

	/**
	 * 下面每一个定义的枚举实例,实际上都经过了这样一段代码: public static WeekDays Monday=new
	 * WeekDays("Monday",0);
	 * 
	 * Monday("Monday",0);
	 * 
	 * class Monday extends WeekDays{ public WeekDays next(){
	 * 
	 * } }
	 * 
	 */
	Monday("星期一") {
		public WeekDays next() {
			return Tuesday;
		}
	},
	Tuesday("星期二") {
		public WeekDays next() {
			return Wednesday;
		}
	},
	Wednesday("星期三") {
		public WeekDays next() {
			return Thursday;
		}
	},
	Thursday("星期四") {
		public WeekDays next() {
			return Friday;
		}
	},
	Friday("星期五") {
		public WeekDays next() {
			return Saturday;
		}
	},
	Saturday("星期六") {
		public WeekDays next() {
			return Sunday;
		}
	},
	Sunday("星期日");

	/**
	 * private WeekDays(){ super(name,ordinal); System.out.println("构造方法"); }
	 */
	// private WeekDays(){
	// System.out.println("构造方法。。");
	// }

	/**
	 * 继承方法
	 */
	public WeekDays next2() {
		/**
		 * 可以使用枚举参与switch,但是实际上,比较的仍然是下标
		 */
		switch (this) {
		case Monday:
			return WeekDays.Tuesday;
		case Tuesday:
			return WeekDays.Wednesday;
		case Wednesday:
			return WeekDays.Thursday;
		case Thursday:
			return WeekDays.Friday;
		case Friday:
			return WeekDays.Saturday;
		case Saturday:
			return WeekDays.Sunday;
		default:
			return WeekDays.Monday;
		}
		// switch (this.ordinal()) {
		// case 0:
		// return WeekDays.Tuesday;
		// case 1:
		// return WeekDays.Wednesday;
		// case 2:
		// return WeekDays.Thursday;
		// case 3:
		// return WeekDays.Friday;
		// case 4:
		// return WeekDays.Saturday;
		// case 5:
		// return WeekDays.Sunday;
		// default:
		// return WeekDays.Monday;
		// }
	}

	/**
	 * 为枚举增加构造方法
	 */
	private WeekDays(String chineseName) {
		this.chineseName = chineseName;
	}

	/**
	 * 给枚举类型增加了属性
	 */
	private String chineseName;

	public WeekDays next() {
		return Monday;
	}

	/**
	 * 给枚举类型增加了方法
	 */
	public String getChineseName() {
		return chineseName;
	}

	@Override
	public String toString() {
		return this.chineseName;
	}

}

你可能感兴趣的:(java-枚举示例)