如果我在编译时不知道枚举的值,如何获取枚举的值?
2022-09-02 12:36:45
我正在尝试执行以下操作:
Class<?> cls = unknownClass;
if(cls.isEnum()){
@SuppressWarnings("unchecked")
Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) cls;
Object val = Enum.valueOf(enumClass, "NAME1");
}
但是我得到以下错误:
Bound mismatch: The generic method valueOf(Class<T>, String) of type Enum<E> is
not applicable for the arguments (Class<capture#5-of ? extends Enum<?>>, String).
The inferred type capture#5-of ? extends Enum<?> is not a valid substitute for
the bounded parameter <T extends Enum<T>>
有人能告诉我我做错了什么吗?