程序员SpringBoot极简教程 · Spring Boot

Java 枚举

2018-01-20  本文已影响54人  聪明的奇瑞

枚举介绍

枚举例子

基本使用,通过 enum 关键字创建枚举

public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
    Day day = Day.MONDAY;
    System.out.println(day);   //MONDAY
}

反编译枚举生成的类

//反编译Day.class
final class Day extends Enum
{
    //编译器为我们添加的静态的values()方法
    public static Day[] values()
    {
        return (Day[])$VALUES.clone();
    }
    //编译器为我们添加的静态的valueOf()方法,注意间接调用了Enum也类的valueOf方法
    public static Day valueOf(String s)
    {
        return (Day)Enum.valueOf(com/zejian/enumdemo/Day, s);
    }
    //私有构造函数
    private Day(String s, int i)
    {
        super(s, i);
    }
     //前面定义的7种枚举实例
    public static final Day MONDAY;
    public static final Day TUESDAY;
    public static final Day WEDNESDAY;
    public static final Day THURSDAY;
    public static final Day FRIDAY;
    public static final Day SATURDAY;
    public static final Day SUNDAY;
    private static final Day $VALUES[];

    static 
    {    
        //实例化枚举实例
        MONDAY = new Day("MONDAY", 0);
        TUESDAY = new Day("TUESDAY", 1);
        WEDNESDAY = new Day("WEDNESDAY", 2);
        THURSDAY = new Day("THURSDAY", 3);
        FRIDAY = new Day("FRIDAY", 4);
        SATURDAY = new Day("SATURDAY", 5);
        SUNDAY = new Day("SUNDAY", 6);
        $VALUES = (new Day[] {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
        });
    }
}

Switch 判断枚举

public static void main(String[] args) {
    Day day = Day.MONDAY;
    switch (day) {
        case MONDAY:
            System.out.println("星期一");
            break;
        case FRIDAY:
            System.out.println("星期五");
            break;
    }
}

为枚举对象创建属性与方法

public enum Day {
    MONDAY("星期一",1),
    TUESDAY("星期二",2),
    WEDNESDAY("星期三",3),
    THURSDAY("星期四",4),
    FRIDAY("星期五",5),
    SATURDAY("星期六",6),
    SUNDAY("星期天",7);

    private String name;
    private int index;

    Day(String name, int index) {
        this.name = name;
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
public static void main(String[] args) {
    Day day = Day.MONDAY;
    System.out.format("%s %s", day.getName(), day.getIndex());    //星期一 1
}

实现接口,所有枚举都继承自 Enum 类,由于 Java 不支持多继承,所以枚举对象不能再继承其他类

public enum Day implements Behaviour {
    MONDAY("星期一",1),
    TUESDAY("星期二",2),
    WEDNESDAY("星期三",3),
    THURSDAY("星期四",4),
    FRIDAY("星期五",5),
    SATURDAY("星期六",6),
    SUNDAY("星期天",7);



    private String name;
    private int index;

    Day(String name, int index) {
        this.name = name;
        this.index = index;
    }

    @Override
    public void print() {
        System.out.format("%s %s",this.name,this.index);
    }
}

使用接口组织枚举

public interface Food {
    enum Coffee implements Food{
        BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO
    }
    enum Dessert implements Food{
        FRUIT, CAKE, GELATO
    }
}
public static void main(String[] args) {
    Food food = Food.Coffee.BLACK_COFFEE;
    System.out.println(food);
}
上一篇下一篇

猜你喜欢

热点阅读