Java的cnum类型
Java的cnum类型
所有的enum类型都是Enum的子类,也继承了相应方法
ordinal()返回枚举值所在的索引位置,从0开始
compareTo()比较两个枚举值的索引位置大小
toString()返回枚举值的字符串表示
valueOf()将字符串初始化为枚举对象
valucs()返回所有的枚举值
package org.example;public class DayTest {public static void main(String[] args){Day d1 = Day.MONDAY;Day d2 = Enum.valueOf(Day.class,"MONDAY");System.out.println(d1 == d2);Day d3= Enum.valueOf(Day.class,"TUESDAY");System.out.println(d1.compareTo(d3));for (Day item: Day.values()) {System.out.println(item.toString()+","+ item.ordinal());}}
}enum Day{MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
}