当前位置: 首页 > 知识库问答 >
问题:

将泛型类型枚举作为参数传递(java)

岳鸿畴
2023-03-14

我写了以下方法

@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");
            }
        }
    } 

我如何将不同的枚举类型传递给第二个参数?我知道我不能创建枚举的实例,但初始化枚举意味着我将传递一个值,而不是整个初始化的枚举,如下所示...其他枚举也将传递给相同的方法以实现组合细节

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;
    }
}

共有1个答案

师曦
2023-03-14

您可能想传递类,而不是枚举实例:

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

这里有一个用法示例:

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);
    }
}
 类似资料:
  • 问题内容: 问题摘要: 我想将具有类型参数(例如)的类作为类型参数传递给泛型方法。 假设我有一个方法: 当然,此方法对于任何类型的类都可以正常使用。我可以这样调用该方法,例如: 问题: 我发现我不能这样做: 从句法上讲,这显然是无效的。但是,我不确定如何实现这样的目标。我当然可以通过,但是泛型类型的添加使其在语法上不再有效,并且我想不出解决方法。 唯一的直接解决方案是这样的事情(看起来很愚蠢):

  • 问题总结:我想传递一个带有类型参数(如

  • 问题内容: 我有一个通用函数,该函数调用Web服务并将JSON响应序列化回一个对象。 我要完成的是等效于此Java代码 我要完成的方法签名正确吗? 更具体地说,将参数类型指定为正确的做法是正确的吗? 调用该方法时,我将其作为returningClass值传递,但是出现编译 错误“无法将表达式的类型’()’转换为’String’类型” CastDAO.invokeService(“test”, wi

  • 所以我试着做下面这样的事情 并且定义为 当尝试编译代码时,clang给出错误。我发现这是因为你不能从lambda推断出类型。 所以这实际上是正确的,因为我实际上希望的参数类型是从的参数推导出的类型。 所以我的问题是,有没有可能排除参数的推导,而只是使用的推导类型?

  • 我想使用泛型类作为另一个泛型类的类型参数。 起初,我对类的定义是这样的: 然后我的需求发生了变化,我不得不为我的R类型使用包装器/持有者类 到目前为止,我的尝试:(给出编译时错误:

  • 问题内容: 这件事让我困扰了一阵子。我之前曾问过一些问题,但措辞可能很拙劣,而且例子太抽象了。所以目前尚不清楚我实际上在问什么。我会再尝试。并且请不要下结论。我希望这个问题根本不容易回答! 为什么我不能在Java中使用带有泛型类型参数的枚举? 问题不在于语法上为什么不可能做到这一点。我知道这只是不受支持。问题是:为什么JSR人员会“忘记”或“忽略”这个非常有用的功能?我无法想象与编译器相关的原因,