Java枚举类-应用例子

Java枚举类-例子

package com.school.stereotype;

/**
 * 活动枚举类型
 * @author QiXuan.Chen
 */
public enum EventStatus {
    /**
     * 未发布。
     */ 
    DRAFT("DRAFT", "未发布"),

    /**
     * 已发布。
     */
    PUBLISHED("PUBLISHED", "已发布");

    /**
     * 活动状态的值。
     */
    private String value;

    /**
     * 活动状态的中文描述。
     */
    private String text;

    /**
     * @param status 活动状态的值
     * @param desc 活动状态的中文描述
     */
    private EventStatus(String status, String desc) {
        value = status;
        text = desc;
    }

    /**
     * @return 当前枚举对象的值。
     */
    public String getValue() {
        return value;
    }

    /**
     * @return 当前状态的中文描述。
     */
    public String getText() {
        return text;
    }

    /**
     * 根据活动状态的值获取枚举对象。
     * 
     * @param status 活动状态的值
     * @return 枚举对象
     */
    public static EventStatus getInstance(String status) {
        EventStatus[] allStatus = EventStatus.values();
        for (EventStatus ws : allStatus) {
            if (ws.getValue().equalsIgnoreCase(status)) {
                return ws;
            }
        }
        throw new IllegalArgumentException("status值非法,没有符合课程状态的枚举对象");
    }

}


你可能感兴趣的:(Java枚举类-应用例子)