Java 枚举

作者: 聪明的奇瑞 | 来源:发表于2018-01-20 13:45 被阅读54次

    枚举介绍

    • 枚举(enum)是 Java5 的新特性,它是一种新类型,用常量来表示特定的数据片段
    • 枚举是通过 enum 关键字创建的,虽与普通 Class 创建类不一样,但通过 enum 创建的类最后依旧会被编译成一个 Class 类,而编译后 Class 类继承了 Enum(枚举抽象类)
    • 当我们在 enum 每创建一个常量时,最后实际上是调用了一次 new Enum<OurClassName> (“MON”,0); 这样的,枚举类型每一个值都将映射到 Enum 的构造函数中,每个值名称被转化为一个字符串,并设置了创建顺序
    • 可以给 enum 自定义属性和方法,并提供构造方法与 set、get 方法,该属性与方法最后都会被编译到动态生成的 Class 类中去

    枚举例子

    基本使用,通过 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);
    }
    

    相关文章

      网友评论

        本文标题:Java 枚举

        本文链接:https://www.haomeiwen.com/subject/ujhtaxtx.html