简体   繁体   English

如何从Java中自己的类返回类的枚举?

[英]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.

相关问题 如何检测Java类是由其自己的main()调用还是从另一个类调用? - How can I detect if a Java class is called by its own main() or from another class? 如何使用自己的类克隆基于对象的数组列表? - How do I clone a object based arraylist with its own class? 如何将main方法分成自己的类? - How do I seperate main method into its own class? 如何制作新的主 class? 已经制作了一个,但不是自己的。java 文件,目前无法使用 - How do I make a new main class? There is already one made but its not its own .java file and currently does not work Java枚举,静态类变量 - 为什么我不能这样做? - Java enums, static class variables - why can't I do this? 如何在Android上的C中加载我自己的Java类? - How do I load my own Java class in C on Android? Java do-while循环在自己类中的方法内部 - Java do-while loop inside a method in its own class 在 Java 中,我如何在它自己的类中引用整个对象? - In Java how would I reference a whole object within a its own class? 如何从特定的 class 创建 object 并使用 java 中的方法更改其属性 - How do I create an object from a specific class and change its attributes by using methods in java 枚举引用Java类 - Enums reference a class Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM