编程语言
首页 > 编程语言> > java EnumExercise枚举方法

java EnumExercise枚举方法

作者:互联网


public class Eunm_ {
    public static void main(String[] args) {
        Season2 autumn = Season2.AUTUMN;
        //输出枚举对象的名字
        System.out.println(autumn.name());//AUTUMN
        // ordinal()输出该枚举的编号,从0开始
        System.out.println(autumn.ordinal());//2
        //返回枚举数组
        Season2[] valuer = Season2.values();
        for (Season2 i:valuer) {
            System.out.println(i);
        }/*Season2{name='春天'}
        Season2{name='冬天'}
        Season2{name='秋天'}
        Season2{name='夏天'}
        */
        //valueof()将字符串转为枚举对象,如果没有匹配的枚举对象,则报错
        System.out.println(Season2.valueOf("AUTUMN"));
        //compareTo:比较两个枚举对象,两个编号的比对。
        System.out.println(Season2.AUTUMN.compareTo(Season2.SPRING));
    }
}``

标签:java,name,EnumExercise,System,println,枚举,Season2,out
来源: https://www.cnblogs.com/enhaocodenote/p/16187508.html