简体   繁体   中英

How to call implemented method of generic enum in Java?

I am trying pass an enum into a method, iterate over that enums values, and call the method that that enum implements on all of those values. I am getting compiler errors on the part "value.getAlias()". It says "The method getAlias() is undefined for the type E" I have attempted to indicate that E implements the HasAlias interface, but it does not seem to work. Is this possible, and if so, how do I fix the code below to do what I want? The code below is only meant to show my process, it is not my intention to just print the names of the values in an enum, but to demonstate 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());
      }
   }
}

Here is how you bound a type to an enum that implements HasAlias:

<E extends Enum<E> & HasAlias>

Once you make thus change, the rest will work.

The following code compiles and works well because getEnumConstants() is defined on Class. No need to say that this class is the class of an Enum.

public class EnumExtended {

   static interface HasAlias{ String getAlias(); }

   static class Identifier {
      static void identify(Class<? extends HasAlias> enumClass ) {
         for( HasAlias value : enumClass.getEnumConstants()) {
            System.out.println( value.getAlias());
         }
      }
   }

   public static void main( String[] args ) {
      Identifier.identify( Letters.class );
      Identifier.identify( Numbers.class );
   }

   static enum Letters implements HasAlias {
      A("The letter A"),
      B("The letter B");

      private final String alias;

      @Override
      public String getAlias(){return alias;}

      Letters(String alias1) {
        this.alias = alias1;
      }
   }

   static enum Numbers implements HasAlias {
      ONE("The number one"),
      TWO("The number two");

      private final String alias;

      @Override public String getAlias(){return alias;}

      Numbers(String alias1) {
        this.alias = alias1;
      }
   }
}

I found the answer in this other post

By referencing the above post, I was able to change my code to the following to make it work:

public <E extends Enum<?> & HasAlias> void identify(Class<E> enumClass)
{
    for(E value : enumClass.getEnumConstants())
    {
     System.out.println(value.getAlias());
    }
}

Yes you must declare your function in an Interface. In order to be an interface it must contain abstract classes ie functions with no definition.

So here you have forgotten to declare your function correctly.

 String getAlias();

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