[英]How do I return a class' enums from its own class in Java?
I want to enforce enum implementation in Java but I found out that I couldn't do it. 我想在Java中强制执行枚举实现,但发现我做不到。 So I decided to define an abstract function that returns the enums of subclasses.
因此,我决定定义一个抽象函数,该函数返回子类的枚举。 But don't know how to do it.
但是不知道该怎么做。
Here is the code: 这是代码:
EnumInterface EnumInterface
public interface EnumInterface
{
public String getString();
}
ParentClass 父类
public abstract class ParentClass {
public abstract Enum<?> getEnums();
}
ChildClass ChildClass
public class ChildClass extends ParentClass{
public enum EnumImplementation implements EnumInterface
{
FOO("foo"),
BAR("bar");
String string;
EnumImplementation(String field)
{
this.string = string;
}
@Override
public String getString() {
return string;
}
}
@Override
public Enum<?> getEnums() {
return ?;
}
}
The code above doesn't work, I'm just trying to describe my problem. 上面的代码不起作用,我只是想描述我的问题。 I also want to enforce the enum return type to
EnumInterface
if possible. 如果可能,我还想将枚举返回类型强制为
EnumInterface
。 If you know how to enforce specific enum implementation that would be better as I don't even have to define the function in ParentClass
. 如果您知道如何执行特定的枚举实现,那将更好,因为我什至不必在
ParentClass
定义函数。
So, how do I return the enum so that I can just do this instanceOfParentClass.getEnums().FOO
? 那么,如何返回该枚举,以便可以只执行
instanceOfParentClass.getEnums().FOO
?
What about this: 那这个呢:
public <T extends Enum<T> & EnumInterface> T[] getEnums() {
return (T[])EnumImplementation.values();
}
Note the cast, which might result in a ClassCastException
. 请注意转换,这可能会导致
ClassCastException
。 To prevent this you could pass the enum class or just return an array of EnumInterface
: 为了防止这种情况,您可以传递枚举类或仅返回
EnumInterface
数组:
public <T extends Enum<T> & EnumInterface> T[] getEnums(Class<T> enumType)
public <T extends Enum<T> & EnumInterface> EnumInterface[] getEnums()
Note that this doesn't enable you to call the method like getEnums().FOO
, but you could pass the class and the name, eg 请注意,这不能使您调用诸如
getEnums().FOO
之类的方法,但是您可以传递类和名称,例如
public <T extends Enum<T> & EnumInterface> T[] getEnum(Class<T> enumType, String enumName) {
return Enum.valueOf( enumType, enumName);
}
However, just as Tim B said, there might be a better option for what you're trying to achieve. 但是,正如Tim B所说,对于您要实现的目标可能会有更好的选择。
Try this. 尝试这个。
EnumInterface EnumInterface
public interface EnumInterface {
public String getString();
}
ParentClass 父类
public abstract class ParentClass<ENUM_TYPE extends Enum<ENUM_TYPE> & EnumInterface> {
public abstract ENUM_TYPE getEnums();
}
ChildClass ChildClass
public class ChildClass extends ParentClass<ChildClass.EnumImplementation> {
public static enum EnumImplementation implements EnumInterface {
FOO("foo"),
BAR("bar");
String string;
EnumImplementation(String field) {
this.string = field;
}
@Override
public String getString() {
return string;
}
}
public EnumImplementation getEnums() {
return EnumImplementation.values()[0];
}
public static void test() {
Object result = new ChildClass().getEnums().FOO;
}
}
Note that getEnums()
returns an enum value. 请注意,
getEnums()
返回一个枚举值。 It's not possible to return the enum container itself, but you can call other enum values from any value (fe EnumImplementation.FOO.BAR.FOO
) 无法返回枚举容器本身,但是您可以从任何值调用其他枚举值(fe
EnumImplementation.FOO.BAR.FOO
)
In order to map property names to database field you can use the enum valueOf method. 为了将属性名称映射到数据库字段,可以使用枚举valueOf方法。
You need to do the mapping in your class as you cannot pass a reference to the static enumeration: 您需要在类中进行映射,因为您无法传递对静态枚举的引用:
http://www.tryjava8.com/app/snippets/52b86150e4b0f5090255bc45 http://www.tryjava8.com/app/snippets/52b86150e4b0f5090255bc45
import java.util.*;
public class Main{
static enum TestE {
FOO,
BAR
}
static class TestC {
TestE getEnum(String name) {
return TestE.valueOf(name);
}
}
public static void main(String[] args){
System.out.println(TestE.FOO);
System.out.println(new TestC().getEnum("BAR"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.