日期:2014-05-20  浏览次数:20750 次

急!!! 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() + &quot;.&quot;
                + 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 &gt;= 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(&quot;EventManageServiceImpl: 事件[ID=&quot;+eventId+&quot;]已经被其他人删除.&quot;);
                    break;
                default:
                    //执行删除操作
                    System.out.println(&quot;EventManageServiceImpl: 事件[ID=&quot;+eventId+&quot;]已经找到,即将被删除...&quot;);
                    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();作用不清楚。