读书人

急 Java 枚举 Enum -Swith.Case怎样操

发布时间: 2012-03-19 22:03:04 作者: rapoo

急!!! Java 枚举 Enum --Swith..Case怎样操作枚举 求解?

Java code
<p>    <span style="font-family:Microsoft YaHei;font-size:13px;">求高手讲解:</span></p><p>    <span style="font-family:Microsoft YaHei;font-size:13px;">我想通过使用switch...case来判断客户端传递的值,但不知道怎样使用这个枚举类,该枚举类定义如下:</span></p><pre class="java" name="code">package ts.bca.meta;import java.util.Arrays;import util.enumerate.base.EnumUtil;import util.enumerate.base.IntegerEnumTypeImp;/** * 流程定义中的任务类型枚举类 *  * @category * @version $Revision: 1.3 $ Mar 25, 2010 * @date Mar 25, 2010 3:39:44 PM */public class EmergencyInfoReportStatesEnum extends IntegerEnumTypeImp {    /**     * serialUID.     */    private static final long serialVersionUID = -1L;    /**     * @deprecated     */    public EmergencyInfoReportStatesEnum() {        super();    }    /**     * @param storeValue 存储值.         ?     */    private EmergencyInfoReportStatesEnum(int storeValue) {        super(storeValue, EmergencyInfoReportStatesEnum.class.getName() + "."                + storeValue, SystemEnumUtil.resource);    }    /** 空值型. */    public static final EmergencyInfoReportStatesEnum NULL = new EmergencyInfoReportStatesEnum();    /**     * 1 报送中     */    final public static EmergencyInfoReportStatesEnum REPORTING = new EmergencyInfoReportStatesEnum(1);    /**     * 2 已报送     */    final public static EmergencyInfoReportStatesEnum REPORT_END = new EmergencyInfoReportStatesEnum(2);    /**     * 3 评估中     */    final public static EmergencyInfoReportStatesEnum ASSESS = new EmergencyInfoReportStatesEnum(3);    /**     * 4 已评估     */    final public static EmergencyInfoReportStatesEnum ASSESS_END = new EmergencyInfoReportStatesEnum(4);    /**     * 5 已删除     */    final public static EmergencyInfoReportStatesEnum DELETED = new EmergencyInfoReportStatesEnum(5);    /**     * 6 已关闭     */    final public static EmergencyInfoReportStatesEnum CLOSED = new EmergencyInfoReportStatesEnum(6);        private static final EmergencyInfoReportStatesEnum[] ALL = {REPORTING, REPORT_END,ASSESS,ASSESS_END,DELETED,CLOSED};    static {        Arrays.sort(ALL);    }    /**     * 取得所有枚举     *      * @return     */    public static EmergencyInfoReportStatesEnum[] getAll() {        return ALL;    }    /**     * 根据键值取得枚举.     *      * @param code     * @return     */    public static final EmergencyInfoReportStatesEnum fromIntCode(final int code) {        int pos = EnumUtil.search(ALL, code);        return (pos >= 0) ? ALL[pos] : null;    }}</pre><p>    <br />    <span style="font-family:Microsoft YaHei;font-size:13px;">客户端传递过来的是一个int类型的值:</span></p><p>    <span style="font-family:Microsoft YaHei;font-size:13px;">我这样写的:</span></p><pre class="java" name="code">            int code=event.getStates();            switch(code){                case 5:                    System.out.println("EventManageServiceImpl: 事件[ID="+eventId+"]已经被其他人删除.");                    break;                default:                    //执行删除操作                    System.out.println("EventManageServiceImpl: 事件[ID="+eventId+"]已经找到,即将被删除...");                    event.setStates(5);                    break;            }</pre><p>    <br />    <span style="font-family:Microsoft YaHei;font-size:13px;">这根本就没有使用枚举呀....求高手讲讲,非常感谢!!!</span></p>


[解决办法]
Java Switch 中的参数可以是byte, short, char, int, enum, String(JDK1.7)。

你的这个EmergencyInfoReportStatesEnum不是个enum!

这行int code=event.getStates();作用不清楚。



如果定要个enum,自己定义一个enum。

示例:

Java code
public enum StateEnum {    NULL(EmergencyInfoReportStatesEnum.NULL),    REPORTING(EmergencyInfoReportStatesEnum.REPORTING),    REPORT_END(EmergencyInfoReportStatesEnum.REPORT_END),    ASSESS(EmergencyInfoReportStatesEnum.ASSESS),    ASSESS_END(EmergencyInfoReportStatesEnum.ASSESS_END),    DELETED(EmergencyInfoReportStatesEnum.DELETED),    CLOSED(EmergencyInfoReportStatesEnum.CLOSED);        private final EmergencyInfoReportStatesEnum value;        StateEnum(EmergencyInfoReportStatesEnum value){        this.value = value;    }        public static StateEnum getState(int code){        EmergencyInfoReportStatesEnum state = EmergencyInfoReportStatesEnum.fromIntCode(code);        if(state == null)return StateEnum.NULL;                for(StateEnum se : StateEnum.values()){            if(se.value == state){                return se;            }        }                return StateEnum.NULL;    }        public static void main(String[] args){//        StateEnum state = StateEnum.getState(event.getStates()));          StateEnum state = StateEnum.getState(1);                switch (state) {        case REPORTING:            System.out.println(state.name()); break;        case REPORT_END:            System.out.println(state.name()); break;        case ASSESS:            System.out.println(state.name()); break;        case ASSESS_END:            System.out.println(state.name()); break;        case DELETED:            System.out.println(state.name()); break;        case CLOSED:            System.out.println(state.name()); break;        default:            System.out.println(state.name());            break;        }    }} 

读书人网 >J2SE开发

热点推荐