Java Enum 底层原理

心不动则不痛 提交于 2019-12-04 12:07:03

public enum WeekDay{


    Mon("Monday"), Tue("Tuesday"), Wed("Wednesday"), Thu("Thursday"), Fri( "Friday"), Sat("Saturday"), Sun("Sunday"); 
     private final String day; 
     private WeekDay(String day) { 
            this.day = day; 
     } 
    public static void printDay(int i){ 
       switch(i){ 
           case 1: System.out.println(WeekDay.Mon); break; 
           case 2: System.out.println(WeekDay.Tue);break; 
           case 3: System.out.println(WeekDay.Wed);break; 
           case 4: System.out.println(WeekDay.Thu);break; 
           case 5: System.out.println(WeekDay.Fri);break; 
           case 6: System.out.println(WeekDay.Sat);break; 
           case 7: System.out.println(WeekDay.Sun);break; 
           default:System.out.println("wrong number!"); 
         } 
     } 
    public String getDay() { 
        return day; 
     } 

    public static void main(String []args){

        for(int i=1;i <=7; i++){

            printDay(i);
        }

    } 

}

WeekDay (javap WeekDay) 经过反编译之后的代码如下:

public final class WeekDay extends java.lang.Enum{ 
    public static final WeekDay Mon; 
    public static final WeekDay Tue; 
    public static final WeekDay Wed; 
    public static final WeekDay Thu; 
    public static final WeekDay Fri; 
    public static final WeekDay Sat; 
    public static final WeekDay Sun; 
    static {}; 
    public static void printDay(int); 
    public java.lang.String getDay(); 
    public static WeekDay[] values(); 
    public static WeekDay valueOf(java.lang.String); 
}
public final class WeekDay extends java.lang.Enum<WeekDay> {
  public static final WeekDay Mon;
  public static final WeekDay Tue;
  public static final WeekDay Wed;
  public static final WeekDay Thu;
  public static final WeekDay Fri;
  public static final WeekDay Sat;
  public static final WeekDay Sun;
  public static WeekDay[] values();
  public static WeekDay valueOf(java.lang.String);
  public static void printDay(int);
  public java.lang.String getDay();
  public static void main(java.lang.String[]);
  static {};
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!