Java——枚举

前言: enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中。
练习代码:

public class EnumTest02 {

    public enum Weekday implements Behaviour {

        SUN("No1",0),
        MON("No2",1),
        TUS("No3",2),
        WED("No4",3),
        THU("No5",4),
        FRI("No6",5),
        SAT("No7",6);

        private int indexValue;
        private String typeName;

        private Weekday(int indexValue){
            this.indexValue = indexValue;
        }
        Weekday(String typeName,int indexValue){
            this.typeName = typeName;
            this.indexValue = indexValue;
        }

        public String getTypeName() {
            return this.typeName;
        }

        public int getValue() {
            return indexValue;
        }
        public static Weekday getNextDay(Weekday nowDay){
            int nextDayValue = nowDay.indexValue;

            if (++nextDayValue == 7){
                nextDayValue =0;
            }

            return getWeekdayByValue(nextDayValue);
        }

        public static Weekday getWeekdayByValue(int value) {
            for (Weekday c : Weekday.values()) {
                if (c.indexValue == value) {
                    return c;
                }
            }
            return null;
        }

        public static Weekday getWeekdayByName(String typeName){
            for(Weekday k : Weekday.values()){
                if(k.getTypeName().equals(typeName)){
                    return k;
                }
            }
            return null;
        }

        public static int getTypeName (String type){
            for(Weekday k : Weekday.values()){
                if(k.toString().equals(type)){
                    return k.ordinal();
                }
            }
            return 0;
        }
        @Override
        public void print() {
            // TODO Auto-generated method stub
            System.out.println(this.indexValue + ":" + this.name());
        }
        @Override
        public String getInfo() {
            // TODO Auto-generated method stub
            return this.typeName;
        }
    }

    public interface Behaviour {
        void print();

        String getInfo();
    }

    //使用枚举创建的单例模式
    public enum EasySingleton{
        INSTANCE;
    }


    public static void main(String[] args) {
        System.out.println("nowday ====> " + Weekday.SAT);
        System.out.println("nowday int ====> " + Weekday.SAT.ordinal());
        System.out.println("nextday ====> " + Weekday.getNextDay(Weekday.SAT));
        System.out.println("nowday name ====> " + Weekday.SAT.typeName);
        System.out.println("nowday name wait10====> " + Weekday.SAT.typeName);
        System.out.println("nowday get indexValue ====> " + Weekday.getWeekdayByValue(6));
        System.out.println("nowday get typeName ====> " + Weekday.getWeekdayByName("No6"));
        System.out.println("nowday get indexValue2 ====> " + Weekday.getTypeName("FRI"));
        System.out.println(Weekday.valueOf("mon".toUpperCase()));

        for (Weekday w : Weekday.values()){
            System.out.println(w + ".ordinal()  ====>" +w.ordinal());
        }
        System.out.println("Weekday.MON.compareTo(Weekday.FRI) ===> " + Weekday.MON.compareTo(Weekday.FRI));
        System.out.println("Weekday.MON.compareTo(Weekday.MON) ===> " + Weekday.MON.compareTo(Weekday.MON));
        System.out.println("Weekday.MON.compareTo(Weekday.SUM) ===> " + Weekday.MON.compareTo(Weekday.SUN));

        System.out.println("Weekday.MON.name() ====> " + Weekday.MON.name());
        System.out.println("<===========================================> ");
        System.out.println("Weekday.valueOf() ====> " + Weekday.valueOf("SUN"));
        System.out.println("Weekday.values()返回包括所有枚举变量的数组 ====> " + Weekday.values());
        System.out.println("Weekday.ordinal()返回枚举类的下标,该次序从0开始 ====>" + Weekday.MON.ordinal());
        System.out.println("Weekday.compareTo() 比较两个枚举变量的”大小”,实际上比较的是两个枚举变量的次序,返回两个次序相减后的结果,如果为负数,就证明变量1”小于”变量2 (变量1.compareTo(变量2),返回【变量1.ordinal() - 变量2.ordinal()】) ====>" + Weekday.MON.compareTo(Weekday.FRI));

        System.out.println("implements 实现1========>" + Weekday.MON.getInfo());
        Weekday.MON.print();

    }
}

总结:写法简洁,不但有内置的方法还可以自定义方法。覆载(Override)了toString和valueOf方法。可以实现接口但不能继承。因为限定了参数的个数。对调用者更严格。是系统更安全。写的单例模式更简洁、线程安全 、不会因为序列化而产生新实例、防止反射攻击。

猜你喜欢

转载自blog.csdn.net/qq_23303245/article/details/81206221