public class T4_EnumMethod { public static void main(String[] args) { WeekDay day1 = WeekDay.FIR; System.out.println(day1);// 直接打印,枚举底层重写了toString() System.out.println(day1.name());// 同上,得到枚举toString后的名字 System.out.println(day1.ordinal());// 排行,在枚举中排第几 System.out.println(day1.getClass());// 得到枚举类的对象 System.out.println(day1.compareTo(WeekDay.WEN));//两个枚举对象比较 相减操作,前减后 System.out.println(WeekDay.valueOf("SUM"));// 将字符串转换成枚举对象 System.out.println(WeekDay.values().length);// 将枚举所有对象转换成数组,就可以进行数组操作 } public enum WeekDay { SUM, MON, TUES, WEN, THIR, FIR, SAT; } }
控制台输出结果:
FIR
FIR
5
class com.day1.T4_EnumMethod$WeekDay
2
SUM
7