当我编译我的 java Enum Day 时,它会生成他的 Day.class 文件和 8 Day$#.class 文件,所以我想知道为什么编译器生成 8 $#.class 而不是 7,因为我有 7 个枚举常量,但是8个覆盖注释,我知道Dollar.class文件是为每个内部类或枚举常量生成的,但是八个.class文件是为了什么而生成的呢?
package com.kaissersoft.test.objective.one.three.enums;
public enum Day{
SUNDAY(01){
@Override
public void salute(){
System.out.println("Today is Sunday");
}
},
MONDAY(02){
@Override
public void salute(){
System.out.println("Today is Monday");
}
},
TUESDAY(03){
@Override
public void salute(){
System.out.println("Today is Tuesday");
}
},
WEDNESDAY(04){
@Override
public void salute(){
System.out.println("Today is Wednesday");
}
},
THURSDAY(05){
@Override
public void salute(){
System.out.println("Today is Thursday");
}
},
FRIDAY(06){
@Override
public void salute(){
System.out.println("Today is Friday");
}
},
SATURDAY(07){
@Override
public void salute(){
System.out.println("Today is Saturday");
}
};
int dayNumber;
Day(final int day){
dayNumber = day;
}
public int getDayNumber(){
return dayNumber;
}
@Override
public String toString(){
switch(this){
case SUNDAY:
System.out.println("Sunday("+this.getDayNumber()+")");
break;
case MONDAY:
System.out.println("Monday("+this.getDayNumber()+")");
break;
case TUESDAY:
System.out.println("Tuesday"+this.getDayNumber()+")");
break;
case WEDNESDAY:
System.out.println("Wednesday("+this.getDayNumber()+")");
break;
case THURSDAY:
System.out.println("Thursday("+this.getDayNumber()+")");
break;
case FRIDAY:
System.out.println("Friday("+this.getDayNumber()+")");
break;
case SATURDAY:
System.out.println("Saturday("+this.getDayNumber()+")");
break;
}
return super.toString();
}
//Abstract method to the day Salute
public abstract void salute();
}
它会生成这个:
- 日班
- 一天$1.class
- Day$2.class
- Day$3.class
- 天$4.class
- 天$5.class
- 天$6.class
- 天$7.class
- 天$8.class