2012-07-30 9 views
8

मैं निम्न विधिकैसे मैं दूसरा तर्क करने के लिए विभिन्न प्रकार के enum पारित कर सकते हैं पैरामीटर (जावा) के रूप में सामान्य प्रकार enum

@SuppressWarnings("unchecked") 
protected <E extends Enum<E>> void populateComboWithEnumValues(Combo combo, E enumData, String defaultSelectionValue) { 

    // populate commbo 
    for (Enum<E> enumVal: enumData.getClass().getEnumConstants()) { 
     combo.add(enumVal.toString()); 
    } 

    // select default selection 
    for (Enum<E> enumVal: enumData.getClass().getEnumConstants()) { 
     if(enumVal.toString().equals(defaultSelectionValue)) { 
      try { 
       combo.select((Integer) enumVal.getClass().getMethod("getSelectionIndex").invoke(enumVal)); 
      } catch (IllegalArgumentException e) { 
       LOGGER.debug("an IllegalArgumentException exception occured"); 
      } catch (SecurityException e) { 
       LOGGER.debug("an SecurityException exception occured"); 
      } catch (IllegalAccessException e) { 
       LOGGER.debug("an IllegalAccessException exception occured"); 
      } catch (InvocationTargetException e) { 
       LOGGER.debug("an InvocationTargetException exception occured"); 
      } catch (NoSuchMethodException e) { 
       LOGGER.debug("an NoSuchMethodException exception occured"); 
      } 
     } 
    } 

लिखा है पारित? मुझे पता है कि मैंने एक enum का उदाहरण नहीं बनाया है, लेकिन एक enum शुरू करने का मतलब है कि मैं एक ही मूल्य गुजर रहा हूं कि पूरे आरंभिक enum इस प्रकार नहीं है ... अन्य enums भी कॉम्बो विनिर्देशों

के लिए एक ही विधि को पारित किया जाएगा
public enum ServerEnvironmentName { 

    /** 
    * The CFD environment name. 
    * Selection Index 
    */ 
    CFD("CFD", 0), 

    /** 
    * The PIT environment name. 
    * Selection Index 
    */ 
    PIT("PIT", 1), 

    /** 
    * The SIT environment name. 
    * Selection Index 
    */ 
    SIT("SIT", 2), 

    /** 
    * The DEV environment name. 
    * Selection Index 
    */ 
    DEV("DEV", 3); 

    /** The input string to identify the environment. */ 
    private String envURL; 

    /** The index value for view selection.*/ 
    private int selectionIndex; 

    /** 
    * Enum constructor to initialise default values. 
    * 
    * @param selectionIndex index value for view selection 
    * @param envURL input parameter for environment 
    */ 
    ServerEnvironmentName(String envURL, int selectionIndex) { 
     this.envURL = envURL; 
     this.selectionIndex = selectionIndex; 
    } 

    /** 
    * Getter for the envURL. 
    * 
    * @return the environment string 
    */ 
    public String getEnvironmentUrl() { 
     return envURL; 
    } 

    /** 
    * This method returns the index of the enum value. 
    * 
    * @return the selection index 
    */ 
    public int getSelectionIndex() { 
     return selectionIndex; 
    } 
} 

उत्तर

40

आप शायद वर्ग पास करना चाहते हैं, न कि किसी enum उदाहरण:

public class EnumTest { 

    protected static <E extends Enum<E>> void enumValues(Class<E> enumData) { 
     for (Enum<E> enumVal: enumData.getEnumConstants()) { 
      System.out.println(enumVal.toString()); 
     } 
    } 

    public static enum TestEnum { 
     ONE, TWO, THREE; 
    } 

    public static void main(String param []) { 
     EnumTest.enumValues(EnumTest.TestEnum.class); 
    } 
} 
:

protected <E extends Enum<E>> void populateComboWithEnumValues(Combo combo, Class<E> enumClass, String defaultSelectionValue) {...} 

यहाँ आप एक के उपयोग का उदाहरण है

संबंधित मुद्दे