Java 具有多态类值的ImmutableMap的类型说明符?
我正在尝试使用Java 具有多态类值的ImmutableMap的类型说明符?,java,oop,guava,factory,factory-pattern,Java,Oop,Guava,Factory,Factory Pattern,我正在尝试使用ImmutableMap创建类类型的命名词典: private static final Map<String, Class<FruitFactory>> REGISTERED_FACTORIES = ImmutableMap.of( "AppleOrchard", AppleFactory.class, "OrangeGrove", OrangeFactory.class ); 和AppleFactory
ImmutableMap
创建类类型的命名词典:
private static final Map<String, Class<FruitFactory>> REGISTERED_FACTORIES =
ImmutableMap.of(
"AppleOrchard", AppleFactory.class,
"OrangeGrove", OrangeFactory.class
);
和AppleFactory
和OrangeFactory
是其实现:
public class AppleFactory implements FruitFactory {
@Override
public Fruit createFruit() {
return new Apple();
}
}
以及:
问题是它无法编译。我从(call)的ImmutableMap.of中得到以下生成错误:
error: incompatible types: inference variable V has incompatible bounds
equality constraints: Class<FruitFactory>
lower bounds: Class<AppleFactory>,Class<OrangeFactory>
错误:不兼容的类型:推理变量V具有不兼容的边界
等式约束:类
下限:类,类
最终,我认为解决方案在于我最初的map声明map REGISTERED\u FACTORIES
中的一些野生泛型语法。我尝试了几次迭代?
,T
,foo extensed bar
,等等,但没有任何效果
有什么想法吗?试试MapSee也谢谢@shmosel!就是这个。我希望Java有一个typedef
。后来,当我在这个地图上迭代时,我有了一个SetHow-about类型的对象。forEach((键,值)->…)
?@shmosel哦,是的!好主意。
public class OrangeFactory implements FruitFactory {
@Override
public Fruit createFruit() {
return new Orange();
}
}
error: incompatible types: inference variable V has incompatible bounds
equality constraints: Class<FruitFactory>
lower bounds: Class<AppleFactory>,Class<OrangeFactory>