How do I call the implementation of generic enumeration in Java?
•
Java
I'm trying to pass an enumeration to a method, iterate over the enumerated values, and call the methods that the enumeration implements on all these values I encountered a compiler error in the "value. Getalias()" section It says "method getalias () is not defined as type e." I try to show that e implements the hasalias interface, but it doesn't seem to work This is possible. If so, how can I fix the following code to do what I want? The following code is just to show my process. I'm not going to print the name of the value in the enumeration, but to prove my problem
public interface HasAlias{ String getAlias(); };
public enum Letters implements HasAlias
{
A("The letter A"),B("The letter B");
private final String alias;
public String getAlias(){return alias;}
public Letters(String alias)
{
this.alias = alias;
}
}
public enum Numbers implements HasAlias
{
ONE("The number one"),TWO("The number two");
private final String alias;
public String getAlias(){return alias;}
public Letters(String alias)
{
this.alias = alias;
}
}
public class Identifier
{
public <E extends Enum<? extends HasAlias>> void identify(Class<E> enumClass)
{
for(E value : enumClass.getEnumConstants())
{
System.out.println(value.getAlias());
}
}
}
Solution
The following is how to bind a type to an enumeration that implements hasalias:
<E extends Enum<E> & HasAlias>
Once you change, the rest will do
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
