枚举类型使用示例

一 枚举类型的定义:

public enum MamsOperateAction {
    NEW(0, "新建"), APPLY_DOWNLOAD(1, "申请下载"), UPLOAD(2, "上传"),
    DOWNLOAD(3, "下载"), MOVE(4, "移动"), CREATE_PREIVEW(5, "生成预览流"),
    RECOVER(6, "还原"), CATALOG(7, "编目"), DELETE(8, "删除"), LEGO_IMPORT(9, "LEGO_IMPORT"),
    SHARE(10, "共享");


    private MamsOperateAction(Integer act, String comment) {
        this.act = act;
        this.comment = comment;
    }

    private Integer act;

    private String comment;

    public Integer getAct() {
        return act;
    }

    public String getComment() {
        return comment;
    }
    public static MamsOperateAction getMamsOperateActionByAct(Integer act){
        for(MamsOperateAction mamsOperateAction : MamsOperateAction.values()){
            if(mamsOperateAction.getAct().equals(act)){
                return mamsOperateAction;
            }
        }
        return null;
    }
    public static MamsOperateAction getMamsOperateActionByAct(String comment){
        for(MamsOperateAction mamsOperateAction : MamsOperateAction.values()){
            if(mamsOperateAction.getComment().equals(comment)){
                return mamsOperateAction;
            }
        }
        return null;
    }
}

二 枚举类型测试使用:

public class MamsOperateActionTest {
    @Test
    public void testEnumMamsOperateAction(){
        MamsOperateAction operateAction = MamsOperateAction.APPLY_DOWNLOAD;//赋值
        MamsOperateAction a = Enum.valueOf(MamsOperateAction.class, "APPLY_DOWNLOAD");//根据字符串名称,返回枚举类型的常量
        MamsOperateAction b = MamsOperateAction.valueOf("APPLY_DOWNLOAD");
        Assert.assertEquals(operateAction,a);
        Assert.assertEquals(a,b);
        Assert.assertEquals(MamsOperateAction.APPLY_DOWNLOAD.name(), "APPLY_DOWNLOAD");//测试枚举类型常量的名称
        Assert.assertEquals(MamsOperateAction.APPLY_DOWNLOAD,MamsOperateAction.getMamsOperateActionByAct(1));//根据act获取指定的枚举类型
    }
}

你可能感兴趣的:(java编程语言)