枚举工具类

news/2024/7/24 3:12:27 标签: enum

从基础开始:

枚举的定义:

public enum SexEnum implements IEnum {

    Male(1, "男"), 
    Female(2, "女"), 
    Unknown(3, "未知");

    private int value;
    private String label;
    

    SexEnum(int value, String label) {
        this.value = value;
        this.label = label;
        
    }
get Set省略。。。。
}

SexEnum 实现了 IEnum 接口,实现接口这个步骤是 关键

public interface IEnum {
    枚举的整型值
    int getValue();
    枚举的中文描述
    String getLabel();
}

然后就是工具类,这里关键就是 getEnumConstants 这个方法

public class EnumUtil {
	    返回指定 值 的'枚举'
		public static <T extends IEnum> T getEnumByValue(Class<T> myclass, int value) {
	        for(T myEnum : myclass.getEnumConstants())
	            if(value == myEnum.getValue())
	                return myEnum;
	        return null;
	    }
		
	    返回指定 名称 的'枚举'
	    public static <T extends IEnum> T getEnumByLable(Class<T> myclass, String lable) {
	        for(T myEnum : myclass.getEnumConstants())
	            if( myEnum.getLabel().equals(lable))
	                return myEnum;
	        return null;
	    }
}

然后就是调用:

EnumUtil.getEnumByValue(AbbEnum.class, 6)


http://www.niftyadmin.cn/n/1868214.html

相关文章

JAVA List 排序

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List;public class People {String name;Integer age;Date birthDate;// ...省略 get(),set(),constructorpublic static void main(String…

redis入门 centos 安装配置

redis 安装记录&#xff1a; 1 安装 https://blog.csdn.net/u010651369/article/details/80677464 2 配置可以远程访问 将安装目录下的 redis.conf 中的 bind去掉 具体操作 vi redis.conf 然后 :/bind 找到bind这一行 然后注释掉 然后 按ESC &#xff0c;然后 :wq …

PSI Probe tomcat监控软件

tomcat-users.xml 添加 <role rolename"manager"/><role rolename"tomcat"/><role rolename"manager-gui"/><user username"tomcat" password"tomcat" roles"manager,tomcat,manager-gui"…

spring security principal credentials authorities details authenticated

spring security在进行认证时&#xff0c;会将用户名和密码封装成一个Authentication对象&#xff0c;在进行认证后&#xff0c;会将Authentication的权限等信息填充完全返回。Authentication会被存在SecurityContext中&#xff0c;供应用之后的授权等操作使用。此处介绍下Auth…

gyp ERR! stack Error: Can‘t find Python executable “python2.7“ npm ERR! node-sass 错误

gyp ERR! stack Error: Cant find Python executable "python2.7", you can set the PYTHON env variable. 相关操作 npm install node-sass //设置全局镜像源&#xff1a; npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/

码工之心

程序的受众不是用户&#xff08;UI的受众是用户&#xff09;&#xff0c;作为一名码工&#xff0c;你的后台编程代码受众是 CPU。

Activiti getEventType()类型总结

startEvent userTask inclusiveGateway--包含网关 exclusiveGateway--排他网关 parallelGateway--并行网关 receiveTask--接收任务 subProcess--内嵌子流程 serviceTask--服务任务 callActivity--链接子流程

Quartz使用技巧总结

一、Quartz 是一个很好的定时器框架&#xff0c;并且支持集群技术。 它的定时任务的配置信息默认是保存在内存中的&#xff0c;这个可以从它的框架默认配置quartz.properties中看出&#xff1a;org.quartz.jobStore.class org.quartz.simpl.RAMJobStore 你也可以复写quartz.p…