【发布时间】:2019-02-15 06:46:02
【问题描述】:
请告诉我。我正在阅读方法参考,其中提到了 lambda 表达式的另一种方法并展示了一些示例,但是当我尝试将自己的谓词转换为方法参考时,我无法做到。
Predicate<String> endsWith= str-> str.endsWith("a");
endsWith.test("asap");
返回值。
如何与方法引用相同的转换??
提前致谢
【问题讨论】:
请告诉我。我正在阅读方法参考,其中提到了 lambda 表达式的另一种方法并展示了一些示例,但是当我尝试将自己的谓词转换为方法参考时,我无法做到。
Predicate<String> endsWith= str-> str.endsWith("a");
endsWith.test("asap");
返回值。
如何与方法引用相同的转换??
提前致谢
【问题讨论】:
您只能在以下情况下使用方法引用:
Predicate endsWith = foo::endsWith;//foo is some variable
Predicate endsWith = FooClass::endsWith;//for static methods
Predicate endsWith = FooClass::endsWith;//this method is not static
【讨论】:
Predicate 用例。方法引用可以有任意数量的参数,只要它与目标接口确定的函数签名相匹配。或者少一个,对于非static 方法,当接收者成为隐式参数时。