定义枚举类

package com.wuxinzhe.demo;
 
interface Info{
    void show();
}
 
public enum Season1 implements Info {
    //1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束
    SPRING("春天", "春暖花开") {
        @Override
        public void show() {
            System.out.println("春天在哪里?");
        }
    },
    SUMMER("夏天", "夏日炎炎") {
        @Override
        public void show() {
            System.out.println("宁夏");
        }
    },
    AUTUMN("秋天", "秋高气爽") {
        @Override
        public void show() {
            System.out.println("秋天不回来");
        }
    },
    WINTER("冬天", "冰天雪地") {
        @Override
        public void show() {
            System.out.println("大约在冬季");
        }
    };
 
    private final String seasonName;
    private final String seasonDesc;
 
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
 
    //4.其他诉求1:获取枚举类对象的属性
    public String getSeasonName() {
        return seasonName;
    }
 
    public String getSeasonDesc() {
        return seasonDesc;
    }
 
}

枚举类测试

    @Test
    void testEnum(){
        Season1[] values = Season1.values();
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i]);
            System.out.println("季节名字:" + values[i].getSeasonName());
            System.out.println("季节名字:" + values[i].getSeasonDesc());
        }
        /** 输出
         * SPRING
         * 季节名字:春天
         * 季节名字:春暖花开
         * SUMMER
         * 季节名字:夏天
         * 季节名字:夏日炎炎
         * AUTUMN
         * 季节名字:秋天
         * 季节名字:秋高气爽
         * WINTER
         * 季节名字:冬天
         * 季节名字:冰天雪地
         */
 
        Season1 spring = Season1.valueOf("SPRING");
        System.out.println(spring);
        System.out.println(spring.getSeasonName());
        /**输出
         * SPRING
         * 春天
         */
 
    }

参考 https://blog.csdn.net/weixin_46072545/article/details/124276541

发表评论

邮箱地址不会被公开。 必填项已用*标注