new WeekDay(){
public WeekDay nextday() {
return SUN;
}
};
这种写法,称为匿名内部类。
//最初的形态
public abstract class WeekDay {
private WeekDay() {} // 私有的构造方法禁止其它类调用
// 定义对象类型为WeekDay的静态常量(final是不可改变的值)。
final static WeekDay sunday = new WeekDay();
final static WeekDay monday = new WeekDay();
....
//调用方法返回后一天对象,this表示当前对象
public WeekDay nextDay(){
if(this==sunday) return monday;
else if(this==monday) return tuesday;
else if(this==tuesday) return wednesday;
....
} |