Java:枚举的通用方法 [英] Java: Generic method for Enums

查看:220
本文介绍了Java:枚举的通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助我了解泛型。假设我有两个枚举作为内部类:

  public class FoodConstants {

public static enum蔬菜{

POTATO,BROCCOLI,SQUASH,CARROT;

}

public static enum Fruit {

APPLE,MANGO,BANANA,GUAVA;

}

}

两个枚举实现一个接口,并且必须实现相同的方法两次,我想在外部类中有一个方法,它的作用如下:

  public< e> String getEnumString<枚举<?> e,String s){

for(Enum en:e.values()){

if(en.name()。equalsIgnoreCase(s)){
return s;
}
}
返回null;
}

但是这种方法不能编译。我想要做的是找出一个字符串值是枚举值的名称,在任何枚举中,无论是蔬菜,水果,什么不是。
无论这是否实际上是一种冗余的方法,我试图(重新)写的是什么问题?



基本上我想这样做:

  public class FoodConstants {

public static enum Vegetable {

POTATO,BROCCOLI,SQUASH,CARROT;

}

public static enum Fruit {

APPLE,MANGO,BANANA,GUAVA;

}


public< e> String getEnumString<枚举<?> e,String s){

for(Enum en:e.values()){

if(en.name()。equalsIgnoreCase(s)){

return s;
}
}
返回null;
}

} //代码结束

感谢你的帮助。 -Alex

解决方案

  public static< E extends Enum&E> 
String getEnumString(Class< E> clazz,String s){
for(E en:EnumSet.allOf(clazz)){
if(en.name()。equalsIgnoreCase ){
return en.name();
}
}
返回null;
}

原来有几个问题:


  1. 它接受一个枚举的实例,而不是表示你想要使用的枚举
    的类。

  2. 不使用type参数。

  3. 返回输入而不是实例名称。也许返回实例会更有用 - 一个不区分大小写的版本的 Enum.valueOf(String)

  4. 它调用一个静态方法在一个实例上,以便您可以迭代。 EnumSet 为您提供所有反思的东西。


Help me understand generics. Say I have two enums as inner classes like so:

public class FoodConstants{

   public static enum Vegetable {

   POTATO,BROCCOLI,SQUASH,CARROT;

  }

   public static enum Fruit {

   APPLE,MANGO,BANANA,GUAVA;

  }

}

Instead of having both enums implement an interface, and have to implement the same method twice, I would like to have a method in the outer class that does something like:

public <e> String getEnumString<Enum<?> e, String s){

 for(Enum en: e.values()){

    if(en.name().equalsIgnoreCase(s)){
   return s;
}
}
return null;
}

However this method does not compile. What I am trying to do is find out if a string value is the name of an enumerated value, in ANY enum, whether it's Vegetable, Fruit, what not. Regardless of whether this is in fact a redundant method, what is wrong with the one I am trying to (re)write?

Basically I would like to do this:

  public class FoodConstants{

       public static enum Vegetable {

       POTATO,BROCCOLI,SQUASH,CARROT;

      }

       public static enum Fruit {

       APPLE,MANGO,BANANA,GUAVA;

      }


      public <e> String getEnumString<Enum<?> e, String s){

          for(Enum en: e.values()){

              if(en.name().equalsIgnoreCase(s)){

            return s;
             }
         }
    return null;
      }

    } //end of code

Thanks for your help. -Alex

解决方案

public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
  for(E en : EnumSet.allOf(clazz)){
    if(en.name().equalsIgnoreCase(s)){
      return en.name();
    }
  }
  return null;
}

The original has a few problems:

  1. It accepts an instance of the enum instead of the class representing the enum which your question suggests you want to use.
  2. The type parameter isn't used.
  3. It returns the input instead of the instance name. Maybe returning the instance would be more useful -- a case-insensitive version of Enum.valueOf(String).
  4. It calls a static method on an instance so you can iterate. EnumSet does all the reflective stuff for you.

这篇关于Java:枚举的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆