Java-枚举

自定义类实现枚举

image-20221206212227125

package cn.meowrain.enum_;

public class Enumeration02 {
    public static void main(String[] args) {
        System.out.println(Season.spring);
    }
}

//演示自定义枚举实现
class Season {
    private String name;
    private String desc;
    //定义了四个对象
    //1.将构造器私有化,防止直接new
//2.去掉setxx方法,防止属性被修改
//3.在Season内部,直接创建固定的对象
    //4.优化,可以加入final
    public static final Season spring = new Season("春天", "温暖");
    public static final Season summer = new Season("夏天", "炎热");
    public static final Season autumn = new Season("秋天", "凉爽");
    public static final Season winter = new Season("冬天", "寒冷");

    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 + '\'' +
                '}';
    }
}

image-20221206214810700

enum关键字实现枚举

package cn.meowrain.enum_;

public class Enumeration03 {
    public static void main(String[] args) {
        System.out.println(Season2.SPRING);
        System.out.println(Season2.SUMMER);
        System.out.println(Season2.AUTTUMN);
        System.out.println(Season2.WINTER);
    }
}

//是同enum关键字实现
enum Season2 {
    //如果使用了enum来实现枚举类
    //1.使用关键字enum替代class
    //2.public static Season spring = new Season("春天", "温暖");使用
    //spring("春天","温暖");替代
    //SPRING("春天", "温暖"); 解读 常量名(实参列表)
    //3.如果有多个常量(对象),使用,分割即可
    SPRING("春天", "温暖"), SUMMER("夏天", "热"), AUTTUMN("秋天", "凉爽"), WINTER("冬天", "寒冷");
    private String name;
    private String desc;

    private Season2(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 "Season2{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

image-20221206214759910

image-20221206214942489

enum常用方法

image-20221206230135628

image-20221206230233486

课堂练习

image-20221206233714381

package cn.meowrain.enum_;

public class Enumeration05 {
    public static void main(String[] args) {
        Week[] days = Week.values();
        for (Week day : days) {
            System.out.println(day.toString());
        }
    }
}

enum Week {
    Monday("星期一"),
    TUESDAY("星期二"),
    WEDNESDAY("星期三"),
    THURSDAY("星期四"),
    FRIDAY("星期五"),
    SATURDAY("星期六"),
    SUNDAY("星期日");
    private String name;

    Week(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

image-20221206233725821

enum实现接口

  1. 使用enum关键字后,就不能再继承其他类了,因为enum会隐式继承Enum,而Java是单继承机制

  2. 枚举类和普通类一样,可以实现接口,如下格式:

    enum 类名 implements 接口1,接口2{}

案例:

package cn.meowrain.enum_;

public class Enumeration06 {
    public static void main(String[] args) {
        Music.CLASSMUSIC.playing();
        Music.POPMUSIC.playing();
    }
}
interface IPlaying {
    public void playing();
}
enum Music implements IPlaying{
    CLASSMUSIC,
    POPMUSIC;
    public void playing() {
        System.out.println("播放好听的音乐" + this.name());
    }
}