enum 里有code,value,type 三个字段,根据type 来获取enum里对相应的的enum值怎么写
时间: 2024-05-08 19:18:55 浏览: 19
可以根据type来遍历整个enum,用一个if语句判断type是否匹配,如果匹配则返回对应的enum值。代码示例:
```python
class MyEnum(Enum):
CODE1 = 1, "value1", "type1"
CODE2 = 2, "value2", "type2"
CODE3 = 3, "value3", "type1"
@classmethod
def get_enum_by_type(cls, type_value):
for enum_value in cls:
if enum_value.value[2] == type_value:
return enum_value
return None
```
使用示例:
```python
enum_type = "type1"
enum_value = MyEnum.get_enum_by_type(enum_type)
if enum_value is not None:
print(enum_value)
else:
print("No enum value found for type: " + enum_type)
```
相关问题
Java enum 里有code,value,type 三个字段,根据type 来获取enum里对相应的的enum值怎么写
enum 类型的变量本质上是常量对象,可以通过对其进行调用来获取其值。如果需要根据type获取相应的enum值,可以使用如下代码:
```
public enum ExampleEnum {
ITEM1("1", "value1", "type1"),
ITEM2("2", "value2", "type2"),
ITEM3("3", "value3", "type1");
private String code;
private String value;
private String type;
private ExampleEnum(String code, String value, String type) {
this.code = code;
this.value = value;
this.type = type;
}
public static ExampleEnum getValueByType(String type) {
for (ExampleEnum exampleEnum : values()) {
if (exampleEnum.type.equals(type)) {
return exampleEnum;
}
}
return null;
}
// getter/setter省略
}
```
这里定义了一个枚举类型,其中每个枚举值都拥有自己的code、value、type三个字段。在 ExampleEnum 类中定义了一个名为 getValueByType 的静态方法,通过传入type参数来获取相应的enum值。
调用方式如下:
```
ExampleEnum exampleEnum = ExampleEnum.getValueByType("type1");
if (exampleEnum != null) {
System.out.println(exampleEnum.getValue()); // 输出 value1
}
```
Result中的实体类对象中有int类型的字段,然后根据枚举类用注解生成另一个文字表示的字段
好的,您的第一个问题是关于在实体类中使用枚举类和注解生成另一个表示字段的问题。
首先,您可以在实体类中定义一个int类型的字段,例如:
```java
public class Result {
private int status;
// other fields and methods
}
```
然后,您可以创建一个枚举类,用于表示不同的状态值:
```java
public enum Status {
SUCCESS(0, "成功"),
FAIL(1, "失败"),
ERROR(2, "错误");
private int code;
private String desc;
Status(int code, String desc) {
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
}
```
接下来,您可以使用注解来生成另一个表示字段,例如:
```java
public class Result {
private int status;
@EnumDesc(Status.class)
private String statusDesc;
// other fields and methods
}
```
在上面的代码中,@EnumDesc是一个自定义的注解,用于生成statusDesc字段的值。然后,您可以定义一个处理器来处理这个注解:
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EnumDesc {
Class<? extends Enum<?>> value();
}
public class EnumDescHandler implements FieldHandler {
@Override
public void handle(Annotation annotation, Field field, Object value) {
if (annotation instanceof EnumDesc) {
EnumDesc enumDesc = (EnumDesc) annotation;
Class<? extends Enum<?>> enumClass = enumDesc.value();
int code = (int) value;
for (Enum<?> enumValue : enumClass.getEnumConstants()) {
if (((Enum<?>) enumValue).ordinal() == code) {
try {
field.setAccessible(true);
field.set(value, enumValue.toString());
} catch (IllegalAccessException e) {
// ignore
}
break;
}
}
}
}
}
```
最后,您可以在Result类的构造函数中使用处理器来生成statusDesc字段的值:
```java
public class Result {
private int status;
@EnumDesc(Status.class)
private String statusDesc;
public Result(int status) {
this.status = status;
FieldHandlerRegistry registry = new FieldHandlerRegistry();
registry.registerHandler(EnumDesc.class, new EnumDescHandler());
registry.handleFields(this);
}
// other fields and methods
}
```
这样,当您创建一个Result对象时,statusDesc字段的值将根据枚举类的定义自动赋值。