我正在尝试对具有在类声明中指定的泛型参数的方法进行方法引用。所以我有:
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> = ...
立即解决问题,但对于这种情况是不可能的。
显然,我做错了什么。但是如何让它发挥作用呢?
谢谢。