简体   繁体   中英

Java: abstract enum constant

Is there a way to declare an abstract enum as a function parameter? What I'm trying to achieve is this:

interface EnumInterpreterInterface {
   String getStringValue(**abstract enum constant** e);
}

interface EnumProviderInterface {
    //classes implementing this interface will hold at least one enum
    Enum getEnum();
}

class EnumProvider implements EnumProviderInterface {
    enum Numbers {ONE, TWO, THREE};
}

class EnumInterpreter implements EnumInterpreterInterface {
   String getStringValue(EnumProvider.Numbers n) {
       switch(e) {
           case EnumProvider.Numbers.ONE: return "one";
           case EnumProvider.Numbers.TWO: return "two";
           default: return "three";
       }
   }
}

If what you want is an abstract Enum you can do this :

interface EnumInterpreterInterface {
    String getStringValue(Enum<?> e);
}

Since Enum is an abstract class it can be used as any class.

But attention has to be made for the implementations, consider this class :

class EnumInterpreter implements EnumInterpreterInterface { code }

Then getStringValue would be implemented like this :

String getStringValue(Enum n) 
{
   if(n instanceof EnumProvider.Numbers)
   {
       EnumProvider.Numbers e=(EnumProvider.Numbers)n;
       switch(e) {
            case ONE: return "one";
            case TWO: return "two";
            default: return "three";
       }
   }
   else //do something, maybe return null or throw an exception
}

Note that you can't change the signature into this String getStringValue(EnumProvider.Numbers n) because the compiler will complain(it wouldn't complain if the enum was the return value though).

And because we can't restrict the Enum type in the signature we have to check with instanceof and cast.

Note

in java enums are classes, that's why the above code works. However, for other programming languages that may not be the case(enums in C#,C++ for example are represented via integers) in which there isn't any way to do this in my humble knowledge :).

It's a bit unclear and please correct me if I'm wrong, but by the switch statement you provided I assume you would want something like making an enum with additional data.

Here is an example:

enum Numbers {

    ZERO("numbers.zero"), ONE("numbers.one"), TWO("numbers.two");

    private String key;

    private Numbers(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }
}

For an enum, each value is an instance of the enum, thus each instance may have different additional properties which are provided in the enum constructor.

Here is how you retrieve the additional data:

System.out.println(Numbers.ZERO.getKey());

You could even have a matching key in a resource bundle file for localization purposes.

Here are references for a tutorial and an interesting article about enums.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM