public class EnumDemo { public static void main(String[] args) { // 1、String name():获取枚举项名称 String name = Season.SPRING.name(); System.out.println(name); // SPRING // 2、int ordinal():返回该枚举项的索引值 int ordinal = Season.SUMMER.ordinal(); System.out.println(ordinal); // 1 // 3、int compareTo():比较两个枚举项的索引值的差值 int compareTo = Season.SUMMER.compareTo(Season.AUTUMN); System.out.println(compareTo); // -1 // 4、String toString():返回枚举项常量的名称,一般用来做打印用, // 如果只想获得枚举项名称,可以使用name()方法 String toString = Season.SUMMER.toString(); System.out.println(toString); // SUMMER // 5、static <T> T calueOf(Class<T> type,String name) // 获取Season枚举类中SUMMER枚举项的对象 Season season = Enum.valueOf(Season.class, "SUMMER"); System.out.println(season); // SUMMER System.out.println(season == Season.SUMMER); // true // 6、valuse():获取所有枚举项 Season[] values = Season.values(); for (Season s : values) { System.out.println(s); } } }
标签:SUMMER,常用,Season,System,枚举,println,方法,out From: https://www.cnblogs.com/weiduaini/p/17228693.html