Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有多态类值的ImmutableMap的类型说明符?_Java_Oop_Guava_Factory_Factory Pattern - Fatal编程技术网

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>