public enum MyEnum { Monday, Tuesday, Wednesday, Thursady, Friday, Saturday, Sunday; public static void main(String[]args){ //Enum 对象 MyEnum mye; mye=MyEnum.Sunday; MyEnum mye1=MyEnum.Monday; /** * enum convert to int * int java.lang.Enum.ordinal() */ System.out.println(mye.ordinal()); System.out.println(mye1.ordinal()); /** * toString() * String java.lang.Enum.name() */ System.out.println(mye); System.out.println(mye.name()); /** * int convert to enum */ MyEnum mye2=MyEnum.values()[1]; MyEnum mye3=mye2.values()[2]; System.out.println(mye2); System.out.println(mye3); /** * is equal or not * boolean java.lang.Enum.equals(Object other) */ System.out.println(MyEnum.Monday.equals(MyEnum.Monday)); /** * foreach out */ System.out.println(mye.values().length); for (MyEnum e : MyEnum.values()) { System.out.println(e); } } }
运行结果:
6
0
Sunday
Sunday
Tuesday
Wednesday
true
7
Monday
Tuesday
Wednesday
Thursady
Friday
Saturday
Sunday
时间: 2024-10-05 23:27:32