枚举基础

简单枚举举例

package com.test;

/**
 * Created by Administrator on 2017/7/31.
 */
public class MainTest {
    public static void main(String[] args) {
        for (TestEnum t : TestEnum.values()){
            System.out.println(t.name() + "---" + t.ordinal() + "---"  + t.getCode());
        }
    }
}
enum TestEnum {
    SPRING("春天"), SUMMER("夏天"), AUTUMN("秋天"), WINTER("冬天");
    private String code;

    private TestEnum(String code){
        this.code = code;
    }
    public String getCode(){
        return code;
    }
}

运行结果:

SPRING---0---春天
SUMMER---1---夏天
AUTUMN---2---秋天
WINTER---3---冬天

package com.test;

/**
 * Created by Administrator on 2017/7/31.
 */
public class MainTest {
    public static void main(String[] args) {
        for (TestEnum t : TestEnum.values()){
            System.out.println(t.name() + "---" + t.ordinal());
        }
    }
}
enum TestEnum {
    RED,GREEN,YELLOW,
}

运行结果:

RED---0
GREEN---1
YELLOW---2

 

posted @ 2017-08-01 00:07  青春不打烊  阅读(108)  评论(0编辑  收藏  举报