枚举类定义如下:
public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败"); private String value; private String desc; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } private Status(String value, String desc) { this.value = value; this.desc = desc; } }
使用方法:
public class StutusTest { public static void main(String[] args) { System.out.println(Status.SCUUESS.getValue()); System.out.println(Status.SCUUESS.getDesc()); System.out.println(Status.FAILED.getValue()); System.out.println(Status.FAILED.getDesc()); } }
再比如,我们在操作数据库的时候,通常使用数字保存到数据库中,但是在界面上显示的时候,需要展示其中文意思,南无我们就可以通过下边的方式:
public enum FlightType { OW(1, "单程"), RT(2, "往返"); public Integer code; public String desc; FlightType(Integer code, String desc) { this.code = code; this.desc = desc; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public static FlightType getTypeByCode(Integer code) { FlightType defaultType = FlightType.OW; for (FlightType ftype : FlightType.values()) { if (ftype.code == code) { return ftype; } } return defaultType; } public static String getDescByCode(Integer code) { return getTypeByCode(code).desc; } }
这样的话,我们就可以通过getDescByCode方法获得其对应的中文。
另外,参数的个数是可以进行变化的,例如下边:
public enum DatabaseType { unknown(null, null, null,null), mysql("com.mysql.jdbc.Driver", "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource", "select 1",Dialect.MYSQL), h2("org.h2.Driver", "org.h2.jdbcx.JdbcDataSource", "select 1",Dialect.H2), oracle("oracle.jdbc.driver.OracleDriver", "oracle.jdbc.xa.client.OracleXADataSource", "select 1 from dual",Dialect.ORACLE); DatabaseType(String driverClassName, String xaDataSourceClassName, String testQuery,Dialect dialect) { this.driverClassName = driverClassName; this.testQuery = testQuery; this.xaDataSourceClassName = xaDataSourceClassName; this.dialect=dialect; } private final String testQuery; private final String driverClassName; private final String xaDataSourceClassName; private final Dialect dialect; public String getDriverClassName() { return driverClassName; } public String getXaDataSourceClassName() { return xaDataSourceClassName; } public String getTestQuery() { return testQuery; } public Dialect getDialect() { return dialect; } public static DatabaseType fromJdbcUrl(String url) { if (StringUtils.hasLength(url)) { Assert.isTrue(url.startsWith("jdbc"), "URL must start with 'jdbc'"); String urlWithoutPrefix = url.substring("jdbc".length()).toLowerCase();
for (DatabaseType driver : values()) { String prefix = ":" + driver.name().toLowerCase() + ":"; if (driver != unknown && urlWithoutPrefix.startsWith(prefix)) { return driver; } } } return unknown; } }
public enum OrderFromSourceEnum implements EnumValue { // 现在(重构后)订单的来源只有 3 开单这一种 PAY_RESERVE(1, "支付预约"), DIRECT_RESERVE(2, "直接预约"), BILL(3, "开单"); private int value; private String name; OrderFromSourceEnum(Integer value, String name) { this.value = value; this.name = name; } /** * 按照Value获得枚举值 */ public static OrderFromSourceEnum valueOf(Integer value) { if (value != null) { for (OrderFromSourceEnum fsEnum : OrderFromSourceEnum.values()) { if (fsEnum.getValue() == value) { return fsEnum; } } } return null; } @Override public int getValue() { return value; } @Override public String getName() { return name; }