码迷,mamicode.com
首页 > 其他好文 > 详细

枚举的使用

时间:2015-06-13 19:55:23      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

package enhance;

/**
 * Created by coolkid on 2014/12/20 0020.
 */
public class EnumTest {
    public static void main(String[] args) {
        WeekDay weekDay = WeekDay.FRI;
        System.out.println(weekDay);
        System.out.println(weekDay.name());
        System.out.println(weekDay.ordinal());//序号
        System.out.println(weekDay.equals(WeekDay.FRI));
        System.out.println(WeekDay.valueOf("SUN"));//字符串转枚举变量
        //迭代枚举类型中的所有元素
        WeekDay[] weekDays = WeekDay.values();
        System.out.println("开始迭代");
        for (WeekDay weekDay1:weekDays){
            System.out.println(weekDay1.ordinal()+":"+weekDay1);
        }
        System.out.println("-------------------------");
        TrafficLamp trafficLamp = TrafficLamp.GREEN;
        System.out.println(trafficLamp);
        System.out.println(trafficLamp.nextLamp());
    }

    public enum WeekDay{
        SUN(1),MON,TUE,WED,THI,FRI,SAT;

        //构造方法
        private WeekDay(){}
        private WeekDay(int day){}
    }

    public enum TrafficLamp{
        RED(30) {
            @Override
            public TrafficLamp nextLamp() {
                return GREEN;
            }
        },
        GREEN(45) {
            @Override
            public TrafficLamp nextLamp() {
                return YELLOW;
            }
        },
        YELLOW(5) {
            @Override
            public TrafficLamp nextLamp() {
                return RED;
            }
        };
        //抽象方法
        public abstract TrafficLamp nextLamp();
        private int time;
        private TrafficLamp(int time){
            this.time = time;
        }

    }
}

 

枚举的使用

标签:

原文地址:http://www.cnblogs.com/sherrykid/p/4573894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!