Enum 枚举基础

1 定义一个枚举

enum Weekend {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

2 得到每个枚举值

 for (Weekend weekend : Weekend.values())

3 返回枚举在集合中顺序(从0开始)

public final int ordinal() {
    return ordinal;
}

4 当前枚举名字

public final String name() {
    return name;
}

5 根据名字返回相应的enum实例

public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                            String name) {
    T result = enumType.enumConstantDirectory().get(name);
    if (result != null)
        return result;
    if (name == null)
        throw new NullPointerException("Name is null");
    throw new IllegalArgumentException(
        "No enum constant " + enumType.getCanonicalName() + "." + name);
}

6 枚举不能被继承(下面的代码报错,提示不能从final的test.Weekday继承)

enum WeekendSon extends Weekend {

}

7 定义enum构造函数

enum Step {  // Instances must be defined first, before methods:
    First("this is the first step"),
    Second("this is the second step"),
    Third("this is the third step"),
    Fourth("this is the fourth step");

    private String description;

    public String getDescription() {
        return description;
    }

    Step(String description) {
        this.description = description;
    }
}

public class AppTest {
    public static void main(String[] args) {
        for (Step step : Step.values()) {
            System.out.println(step + ":" + step.getDescription());
        }
    }
}

输出结果:

First:this is the first step
Second:this is the second step
Third:this is the third step
Fourth:this is the fourth step

8 实例一枚
State接口:

public interface State {
    int getCode();
    String getDesc();
    State nameOf(String var1);
}

LifeState枚举:

public enum LifeState implements State {
    babyState(0, "儿童状态"),
    youngState(1, "年轻状态"),
    adultState(2, "成年状态"),
    oldState(3, "老年状态");

    private int code;
    private String desc;

    private LifeState(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    @Override
    public int getCode() {
        return this.code;
    }

    @Override
    public String getDesc() {
        return this.desc;
    }

    @Override
    public State nameOf(String name) {
        return valueOf(name);
    }

    public static LifeState codeOf(int code) {
        LifeState[] arr = values();

        for(int i = 0; i < arr.length; i++) {
            if(arr[i].code == code) {
                return arr[i];
            }
        }
        return null;
    }
}

Test测试类:

public class Test {
    public static void main(String[] args) {
        LifeState lifeState = LifeState.babyState;
        System.out.println(lifeState + "---" + lifeState.getCode() + "---" + lifeState.getDesc());
        State babyState = lifeState.nameOf("youngState");  //调用valueOf,返回相应的枚举实例
        System.out.println(babyState + "---" + babyState.getCode() + "---" + babyState.getDesc());

        System.out.println(LifeState.codeOf(3).getDesc());

        System.out.println(LifeState.youngState.name());
    }
}

运行结果:

babyState---0---儿童状态
youngState---1---年轻状态
老年状态
youngState
上一篇:C语言关键字之sizeof


下一篇:1.C语言关键字(auto break case char const swtich)