java setter 的 Lambda 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27759948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:20:50  来源:igfitidea点击:

Lambda expression for setter

javalambdasettergetter

提问by Kowser

We have lambda expression for getter as below:

我们有如下 getter 的 lambda 表达式:

Function<Student, String> studentNameGetter = Student::getName;

How about lambda expression for the setter?

setter 的 lambda 表达式怎么样?

回答by Keppil

I'm not sure what you mean by creating a lambda expression for the setter.

我不确定为 setter 创建 lambda 表达式是什么意思。

What it looks like you are trying to do is to assign the method reference to a suitable Functional Interface. In that case, the best match is to a BiConsumer:

看起来您正在尝试做的是将方法引用分配给合适的功能接口。在这种情况下,最佳匹配是 a BiConsumer

BiConsumer<Student, String> studentNameSetter = Student::setName;