我正在从Java迁移到Groovy,并且对方法引用有问题。
在Java中,我可以这样做:
Function<Bean, String> f = Bean::method;
String s = f.apply(new Bean());我想在Groovy中实现相同的功能。我试着做:
Function f = Bean.&method
Sting s = f.apply new Bean()但我有一个例外,在f.apply上:
groovy.lang.MissingMethodException: No signature of method: Bean.method() is applicable for argument types: (Bean) values: [Bean@17483c58]我知道可以执行以下操作来获得实例方法的方法引用,但我希望为任何实例获取一个泛型方法。
MethodClosure f = bean.&method
String s = f()我想用这个来使用EasyBind库。它允许您选择带有函数引用的JavaFX属性。您可能有一个类和属性的层次结构,要选择它们,可以这样做:
property.bind(EasyBind.select(root).select(Root::branch).selectObject(Branch::leaf));因此,当树中的任何值发生变化时,property都会用正确的值进行更新。
我能够用Bean.&method替换{bean -> bean.method},这很好。在Java中,Bean::method实际上是bean -> bean.method的别名类型。
发布于 2016-01-15 17:17:40
您可以使用:
MethodClosure f = { it.method }
String s = f()https://stackoverflow.com/questions/34815708
复制相似问题