匿名内部类抽象实现枚举

/*
 星期天: Sun.=Sunday 星期一: Mon.=Monday 星期二: Tues.=Tuesday 星期三: Wed.=Wednesday 
 星期四: Thur.=Thursday 星期五: Fri.=Friday 星期六: Sat.=Saturday 
 */
public abstract class WeekDayAbstract {
	// 构造函数私有化 不可以在new实例对象
	private WeekDayAbstract() {}
	// 采用匿名内部类的实现方式
	public static final WeekDayAbstract SUNDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return MONDAY;
		}
	};
	public static final WeekDayAbstract MONDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return TUESDAY;
		}
	};
	public static final WeekDayAbstract TUESDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return WEDDAY;
		}
	};
	public static final WeekDayAbstract WEDDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return THURDAY;
		}
	};
	public static final WeekDayAbstract THURDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return FRIDAY;
		}
	};
	public static final WeekDayAbstract FRIDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return SATDAY;
		}
	};
	public static final WeekDayAbstract SATDAY = new WeekDayAbstract() {
		public WeekDayAbstract NextDay() {
			return SUNDAY;
		}
	};
	// 定义NextDay()方法
	public abstract WeekDayAbstract NextDay();
	public String toString() { // 重写ToString方法
		return this == MONDAY ? "星期一" : this == TUESDAY ? "星期二"
				: this == WEDDAY ? "星期三" : this == THURDAY ? "星期四"
						: this == FRIDAY ? "星期五" : this == SATDAY ? "星期六"
								: "星期日";
	}
}

你可能感兴趣的:(java枚举enum)