java5的枚举讲解

13.用普通类模拟枚举的实现原理   
  1.私有的构造方法    
  2.每个元素分别用一个公有的静态成员变量表示  
  3.可以有若干个公有方法或者抽象方法,通过不同的实现定义出不同的对象。   
       示例代码:
package java_5;

public abstract class WeekDay {
    private WeekDay() {
    }

    public final static WeekDay MON = new WeekDay() {

          @Override
          public WeekDay preDay() {
              return SUM ;
         }

    };
    public final static WeekDay WEN = new WeekDay() {

          @Override
          public WeekDay preDay() {
              return MON ;
         }
    };
    public final static WeekDay THUR = new WeekDay() {

          @Override
          public WeekDay preDay() {
              // TODO Auto-generated method stub
              return WEN ;
         }
    };
    public final static WeekDay FRI = new WeekDay() {

          @Override
          public WeekDay preDay() {
              return THUR ;
         }
    };
    public final static WeekDay STAR = new WeekDay() {

          @Override
          public WeekDay preDay() {
              return FRI ;
         }
    };
    public final static WeekDay SUM = new WeekDay() {

          @Override
          public WeekDay preDay() {
              return STAR ;
         }
    };

    public abstract WeekDay preDay();

    public WeekDay nextDay() {
          if (this == MON) {
              return WEN ;
         } else if (this == WEN) {
              return THUR ;
         } else if (this == THUR) {
              return FRI ;
         } else if (this == FRI) {
              return STAR ;
         } else if (this == STAR) {
              return SUM ;
         } else {
              return MON ;
         }
    }
}

package java_5;

public class EnumTest {

    public static void main(String[] args) {
         WeekDay weekDay =  WeekDay.MON;
         System. out.println(weekDay.preDay());
         System. out.println(weekDay.nextDay());
    }
}

14.java5的枚举的基本应用15.实现带有构造方法的枚举
16.实现带有抽象方法的枚举
package java_5;

public class EnumTest {
    
    public enum TransLight {
          //枚举元素必须位于枚举体中的最开始的部分,枚举元素用;号与其他成员分开
          YELLOW {

              @Override
              public TransLight nextLight() {
                  return RED ;
             }

         }, //这个可以理解为创建该类的子类对象,在构造函数中调用了父类待参数的构造方法
          //假如枚举中只有一个成员,就可以作为一个单例的实现方式(构造私有,对象一个)
          GREEN(30) {

              @Override
              public TransLight nextLight() {
                  return YELLOW ;
             }

         },
          RED(45) {

              @Override
              public TransLight nextLight() {
                  return GREEN ;
             }
         };
          //枚举可以定义构造方法,成员。构造方法必须私有
          private int time;
         
          private TransLight(int time) {
              this.time = time;
         }
          private TransLight() {
              this.time = 100;
         }

          public abstract TransLight nextLight();

          public void getNam() {
              this.toString();
         }
    }

    public enum Weekday {
          MON, WEN (3), THUR(4), FRI(5), STAR (6), SUN(7);
          private Weekday() {
         }

          private int aa ;

          private Weekday(int aa1) {
              this.aa = aa1;
             System. out.println(aa );
         }
    }

    public static void main(String[] args) {
          method1();
        System. out.println("" .getClass().getSuperclass());
    }

    public static void method2() {
         WeekDay weekDay = WeekDay. MON;
         System. out.println(weekDay.preDay());
         System. out.println(weekDay.nextDay());
    }

    public static void method1() {
         Weekday weekday = Weekday. MON;
        System. out.println(weekday.compareTo(Weekday.WEN));// 比较两个对象的ordinal
         System. out.println(weekday.ordinal());// 找出该对象的在枚举中的位置
        System. out.println(weekday.getDeclaringClass());// 找到声明的类
         System. out.println(weekday.getClass());// 找到当前运行的的类的字节码对象
        System. out.println(weekday.getClass().getSuperclass());// 找到当前运行的的类的父类字节码对象
        System. out.println(Weekday.valueOf( "MON").toString());// 把字符串映射为枚举中的一个对象(静态)
        System. out.println(Weekday.values(). length);// 把枚举中的每个对象封装成一个数组(静态)
    }
}




你可能感兴趣的:(java)