- /**
- 用普通类来模拟枚举 的实现原理
- */
- public class EnumDemo
- {
- public static void main(String [] args)
- {
- WeekDay weekday=WeekDay.Mon;
- System.out.println(weekday.nextDay());
-
- }
- }
- /**
- 私有的构造方法
- 每个元素分别用一个公有的静态成员变量来表示
- 可以有若干 公有方法或 抽象方法 ,如要提供nextDay方法必须是抽象的,采用抽象方法
- 定义nextDay方法就将大量的if-else语句 转移成一个个 独立的类
- */
- public abstract class WeekDay // 这里出现下面图片的错误???
- {
- //私有的构造方法
- private WeekDay(){}
- //每个元素分别用一个公有的静态成员变量来表示
- public static final WeekDay Sun=new WeekDay(){
- //匿名内部类也就是子类 来 继承 这个抽象父类 WeekDay
- public WeekDay nextDay()
- {
- return Mon;
- }
-
- };
- public static final WeekDay Mon=new WeekDay(){
- public WeekDay nextDay()
- {
- return Tue;
- }
-
- };
- public static final WeekDay Tue=new WeekDay(){
-
- public WeekDay nextDay()
- {
- return Wed;
- }
- };
- public static final WeekDay Wed=new WeekDay(){
- public WeekDay nextDay()
- {
- return Thurs;
- }
- };
- public static final WeekDay Thurs=new WeekDay(){
- public WeekDay nextDay()
- {
- return Fri;
- }
- };
- public static final WeekDay Fri=new WeekDay(){
- public WeekDay nextDay()
- {
- return Sat;
- }
- };
- public static final WeekDay Sat=new WeekDay(){
- public WeekDay nextDay()
- {
- return Sun;
- }
- };
-
- //可以有若干 公有方法或 抽象方法
- public abstract WeekDay nextDay();
-
-
- public String toString()
- {
- return null;
- }
-
-
- }
复制代码 这个是为啥?
|