java-无法找出HashMap的正确类型

filter.put("a", EnumA.class);I got following setup:

public interface InterfaceA {
  abstract public Object[] foo();
}

public enum EnumA implements InterfaceA {
  RED();
  ...
}

public enum EnumB implements InterfaceA {
  OAK();
  ...
}

现在我想使用这个构造类型保存,我得到了:

 

private <T extends Enum<T> & InterfaceA> void  importSettingViaEnum(Class<T> clazz) { ...
    for (T elem : clazz.getEnumConstants()){
        ... = f(elem.ordinal());
        ... = elem.foo();
        ...
    }
}

这似乎是正确的,爵士乐团只能工作上述枚举.但是现在,我无法弄清楚这张地图的正确类型,这是行不通的:

 

public <T extends Enum<T> & InterfaceA> Main() {
    Map<String, Class<T>> filter = new HashMap<>();
    filter.put("a", EnumA.class);
    filter.put("b", EnumB.class);
    importSettingViaEnum(filter.get("a"));
}

有人有线索吗?我希望这个东西是类型安全的.

这里有一些pastebin:https://pastebin.com/fKxtBGBe

编辑1:

尝试过类似的操作,但无法正常工作…

 

public Main() {
        Map<String, Class<? extends Enum<? extends InterfaceA>>> filter = new HashMap<>();
        filter.put("a", EnumA.class);
        filter.put("b", EnumB.class);
        importSettingViaEnum(filter.get("a")); // BREAK THE BUILD
    }

最佳答案

类型擦除…提供通用类型参数的类型对象.

 

 

public <T extends Enum<T> & InterfaceA> void main(Class<T> type) {
    Map<String, Class<T>> filter = new HashMap<>();
    filter.put("a", type);
    //importSettingViaEnum(filter.get("a"));
}

    main(EnumA.class);

这也使实现类型(EnumA)分离.

还是为了部分类型的安全

 

public void main2() {
    Map<String, Class<? extends InterfaceA>> filter = new HashMap<>();
    filter.put("a", EnumA.class);
    Class<? extends InterfaceA> type = filter.get("a");
    if (type.isEnum()) {
        ....
    }
}

如果需要,将类型强制转换为Enum.


Inm小程序商店

 

Inm小程序商店收录了最新,最热门的微信小程序和微信小游戏,是国内内容最丰富的集小程序游戏、小程序分发、小程序推广为一体的综合性小程序门户网站之一。


Vultr中文网

 

最低 $2.5/月 的VPS, 稳定, 可靠

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值