编程语言
首页 > 编程语言> > Java基础-枚举enum关键字

Java基础-枚举enum关键字

作者:互联网

package com.hspedu.enum_;

public class Course426 {
    public static void main(String[] args) {
        // enum关键字实现枚举

        /*
         * 1、enum关键字替代class
         * 2、常量名/对象名 + (实参列表); 关联构造器
         * 3、如果有多个常量(对象),使用,间隔
         * 4、使用enum要求将常量对象写在开始的位置
         * */

        System.out.println(Season.AUTUMN);
        System.out.println(Season.SPRING);
        System.out.println(Season.SUMMER);
        System.out.println(Season.WINTER);
    }
}

enum Season {
//    public static final Season SPRING = new Season("春天", "温暖");
//    public static final Season WINTER = new Season("冬天", "寒冷");
//    public static final Season SUMMER = new Season("夏天", "炎热");
//    public static final Season AUTUMN = new Season("秋天", "凉爽");

    SPRING("春天", "温暖"),
    SUMMER("夏天", "炎热"),
    AUTUMN("秋天", "凉爽"),
    WINTER("冬天", "寒冷");

    private String name;

    private String desc;

    private Season(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return "Season{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

 

标签:Java,String,Season,enum,枚举,desc,public,name
来源: https://www.cnblogs.com/kenantongxue/p/15979087.html