我正在尝试使用方法引用来捕获方法调用并且遇到了一些限制。这工作正常:
<T> void capture(Function<T, ?> in) {
}
private interface Foo {
String getBar();
}
capture(Foo::getBar);
但是,如果我将 Foo.setBar 的签名更改为如下所示:
private interface Foo {
void setBar(String bar);
}
capture(Foo::setBar);
我收到一个错误:
Cannot make a static reference to the non-static method setBar(String) from the type MyTest.Foo
我不清楚限制是什么。理想情况下,我想使用方法引用来捕获标准设置器上的调用。有没有办法做到这一点?