[英]Cannot infer functional interface type Java 8
I have a factory(Registry DP) which initializes the class: 我有一个工厂(注册表DP)初始化类:
public class GenericFactory extends AbstractFactory {
public GenericPostProcessorFactory() {
factory.put("Test",
defaultSupplier(() -> new Test()));
factory.put("TestWithArgs",
defaultSupplier(() -> new TestWithArgs(2,4)));
}
}
interface Validation
Test implements Validation
TestWithArgs implements Validation
And in the AbstractFactory 并在AbstractFactory中
protected Supplier<Validation> defaultSupplier(Class<? extends Validation> validationClass) {
return () -> {
try {
return validationClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Unable to create instance of " + validationClass, e);
}
};
}
But I keep getting Cannot infer functional interface type Error. 但我不断得到无法推断功能接口类型错误。 What I am doing wrong here ?
我在这做错了什么?
Your defaultSupplier
method has an argument type of Class
. 您的
defaultSupplier
方法的参数类型为Class
。 You can't pass a lambda expression where a Class
is expected. 您不能传递一个lambda表达式,其中需要一个
Class
。 But you don't need that method defaultSupplier
anyway. 但是你无论如何都不需要那个方法
defaultSupplier
。
Since Test
and TestWithArgs
are a subtypes of Validation
, the lambda expressions () -> new Test()
and () -> new TestWithArgs(2,4)
are already assignable to Supplier<Validation>
without that method: 由于
Test
和TestWithArgs
是Validation
的子类型,因此lambda表达式() -> new Test()
和() -> new TestWithArgs(2,4)
已经可以在不使用该方法的情况下分配给Supplier<Validation>
:
public class GenericFactory extends AbstractFactory {
public GenericPostProcessorFactory() {
factory.put("Test", () -> new Test());
factory.put("TestWithArgs", () -> new TestWithArgs(2,4));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.