【Java学习笔记】枚举类型

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

java.lang.Enum > 使用enum定义。

类如:

public class EnumDemo {
    enum Edge {
        TOP, BOTTOM, LEFT, RIGHT//定义了一个enum类型的实例,一个枚举类型除了那些由枚举常量定义的实例外没有任何其他实例。
    };
    public static void main(String arg[]) {
        Edge edge1 = Edge.LEFT;
        Edge edge2 = Edge.LEFT;
      //由于每个枚举类型的实例在一个程序中只能有一个,所以我们可以使用==来判断是否相同。
        System.out.println("Can we use == to judge equals:"+ (edge1 == edge2));
        int i = edge1.ordinal();
        System.out.println("The value of i: " + i);
        System.out.println(edge1);
        String str = "";
        switch(edge1) {
    /*  case Edge.TOP: This is wrong*/
        case TOP: str = "The top edge"; break;
        case BOTTOM: str = "The bottom edge"; break;
        case LEFT: str = "The left edge"; break;
        case RIGHT: str = "The right edge"; break;
        }
        System.out.println(str);
    }
这就解决了switch不能对字符串进行的缺点了。注意:枚举类型也是类,里面的成员实际上是静态的final类型的枚举类型实例。

为了更好的理解枚举类型,我们用普通类模拟一下该枚举类

public abstract class WeekDay {
    private int workhours;
    private boolean work;
    private WeekDay(int workhours, boolean work) {
        this.workhours = workhours;
        this.work = work;
    }
    protected abstract WeekDay getNextDay();
    public static final WeekDay MONDAY = new WeekDay(8,true){
         public WeekDay getNextDay() {
                return TUESDAY;
            }
         @Override
        public String toString() {
            return "MONDAY";
        }
    };
    public static final WeekDay TUESDAY = new WeekDay(8,true){
         public WeekDay getNextDay() {
                return WEDNESDAY;
            }
         @Override
            public String toString() {
                return "TUESDAY";
            }
    };
    public static final WeekDay WEDNESDAY = new WeekDay(8,true){
         public WeekDay getNextDay() {
                return THURSDAY;
            }
         @Override
            public String toString() {
                return "WEDNESDAY";
            }
    };
    public static final WeekDay THURSDAY = new WeekDay(8,true){
         public WeekDay getNextDay() {
                return FRIDAY;
            }
         @Override
            public String toString() {
                return "THURSDAY";
            }
    };
    public static final WeekDay FRIDAY = new WeekDay(8,true){
         public WeekDay getNextDay() {
                return SATURDAY;
            }
         @Override
            public String toString() {
                return "FRIDAY";
            }
    };
    public static final WeekDay SATURDAY = new WeekDay(4,true){
         public WeekDay getNextDay() {
                return SUNDAY;
            }
         @Override
            public String toString() {
                return "SATURDAY";
            }
    };
    public static final WeekDay SUNDAY = new WeekDay(0,false){
         public WeekDay getNextDay() {
                return MONDAY;
            }
         @Override
            public String toString() {
                return "SUNDAY";
            }
    };
    private static void showDay(WeekDay d) {
        if(d.isWeekday()) {
            System.out.println(d + " is a weekday and has " +
                    d.getHours() + " working hours.");
        } else {
            System.out.println(d + " is not a weekday and has " +
                    d.getHours() + " working hours.");
        }
    }
    private int getHours() {
        // TODO Auto-generated method stub
        return workhours;
    }
    private boolean isWeekday() {
        return work;
    }
    public static void main(String[] args) {
        WeekDay day = WeekDay.FRIDAY;
        showDay(day);
        day = WeekDay.FRIDAY.getNextDay();
        showDay(day);
        day = day.getNextDay();
        showDay(day);
    }
}

因此,有以下更复杂的应用:

public enum Day {
MONDAY (8,true){//首先使用了子类继承的匿名类方式进行了对父类构造函数的,并且在子类中完成了父类的抽象方法
        public Day getNextDay() {
            return TUESDAY;
        }
    },

    TUESDAY (8,true){
        public Day getNextDay() {
            return WEDNESDAY;
        }
    },
    WEDNESDAY (8,true){
        public Day getNextDay() {
            return THURSDAY;
        }
    },
    THURSDAY (8,true){
        public Day getNextDay() {
            return FRIDAY;
        }
    },
    FRIDAY (8,true){
        public Day getNextDay() {
            return SATURDAY;
        }
    },
    SATURDAY (4,false){
        public Day getNextDay() {
            return SUNDAY;
        }
    },
    SUNDAY (0,false){
        public Day getNextDay() {
            return MONDAY;
        }
    };
    private int hours;
    private boolean weekday;
Day(int workHours,boolean weekdayFlag) {
        hours = workHours;
        weekday = weekdayFlag;
    }
    private abstract Day getNextDay();
    public int getHours() {
        return(hours);
    }
    public boolean isWeekday() {
        return(weekday);
    }
    private static void showDay(Day d) {
        if(d.isWeekday()) {
            System.out.println(d + " is a weekday and has " +
                    d.getHours() + " working hours.");
        } else {
            System.out.println(d + " is not a weekday and has " +
                    d.getHours() + " working hours.");
        }
    }
    public static void main(String arg[]) {
        Day day;
        day = Day.TUESDAY;
        showDay(day);
        day = Day.SATURDAY;
        showDay(day);
        day = Day.THURSDAY.getNextDay();
        showDay(day);
    }
}

注意:

1.注意枚举类型不能定义为abstract或者final。

2.枚举类中定义了抽象方法,但是枚举类却没有实例去实现这个抽象方法,这样会报错。

3.一个枚举类型除非包含至少一个枚举常量带有class body,否则就是隐式final的。

4.嵌套 (在一个类中定义的枚举)的枚举类型是隐式static的,若显示赋予就会出错。

5.EnumSet包含了许多静态方法用来指定不同的集合建立和读取方式。

 

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

posted @ 2013-01-04 14:14  gnuhpc  阅读(714)  评论(0编辑  收藏  举报