我想创建一个方法引用来指向在类声明中指定了通用参数的方法。 所以我有:
public interface IExecutable<P extends IParameter> {
void execute(P parameter);
}
public class Parameter implements IParameter {
public void childSpecific() {
...
}
}
public class TestClass {
...
//somewhere in the code
public void foo(Parameter parameter) {
parameter.childSpecific();
}
public void test() {
IExecutable<?> executable = this::foo; //compilation error
// The type TestClass does not define inner(IParameter) that is applicable here
executable.execute(new Parameter()); //compilation error as well
// The method execute(capture#4-of ?) in the type IExecutable<capture#4-of ?> is not applicable for the arguments (Parameter)
}
...
}
我不确定这里可执行文件的具体通用类型。使用
IExecutable<Parameter> = ...
虽然立即解决问题是理想的,但在这种情况下不可能。
显然,我做错了什么。但是如何使它正常工作呢?
谢谢。
IExecutable<IParameter>
能正常工作吗?另外,我觉得你会收到很多有关缺少泛型类型的警告,请尝试修复它们。 - ThiloIExecutable<?>
的引用,您将无法使用除null
字面值以外的任何参数调用execute
。因此,我不明白您想做什么。 - Sotirios Delimanolisthis::foo
:) - ZhongYu