使用setBorn ?尝试:but how would I use the forEach method and use the setBorn using a method reference? Trying: people.forEach(Person::setBorn);导致错误,无法解析方法setBorn。 results in an error, "Cannot resolve method setBorn". 此外,我如何传递True的值? In addition, how would I pass in the value of True? 推荐答案使用lambda:people.forEach((p) -> p.setBorn(true));发现没有其他方法只使用java 8 API。Found no other ways only using the java 8 API.使用此自定义函数:public static <T, U> Consumer<T> bind2(BiConsumer<? super T, U> c, U arg2) { return (arg1) -> c.accept(arg1, arg2);}你可以这样做:people.forEach(bind2(Person::setBorn, true));如果java API或库中提供了这种实用工具方法,请告诉我们。If this kind of utility methods is available in the java API or in a library, please let us know. 这篇关于带参数的方法引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 01:04