泛型通配符不接受实现接口的java类
我希望使用类类型作为键的映射,以避免在访问值时强制转换
更新
下面是您可以复制/粘贴到IDEA中并复制它的实际代码
interface MyInterface {
}
class SomeConcreteClass implements MyInterface{
}
private Map<Class<?>, ? extends MyInterface> map = newMap();
private Map<Class<?>, ? extends MyInterface> newMap() {
Map<Class<?>, ? extends MyInterface> map = new HashMap<>();
map.put(SomeConcreteClass.class, new SomeConcreteClass());
return map;
}
private void accessMap() {
SomeConcreteClass clazz = (SomeConcreteClass) map.get(SomeConcreteClass.class); <== I want to avoid cast here
}
问题:这无法编译。我排在这一行:
map.put(SomeConcreteClass.class, new SomeConcreteClass());
错误:
Wrong 2nd argument type. Found: 'SomeConcreteClass required '? extends MyInterface`
我做错了什么SomeConcreteClass
应该被接受,因为它实现了接口
# 1 楼答案
这可以简化很多:
这背后的原理被称为
PECS
(非常著名)。不允许这样做的原因读起来有点冗长,但主要是解释了here。虽然不明显,但如果允许的话——这可能会在其他地方造成问题,我链接的答案解释了这一点您可以通过所谓的
typesafe heterogeneous container
实现您想要的:其用途是: