简体   繁体   English

Java - setter 和 getter 的方法参考

[英]Java - method reference for setter and getter

A Pojo class:宝卓class:

package com.lambda.jay;

public class Person {
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Another class for initiating 'Person' objects:另一个 class 用于启动“Person”对象:

package com.fi.lambda.jay;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class Actor {
    public <T,R> void printField(T obj, Function<? super T,? extends R> getter1) {
        System.out.println(getter1.apply(obj).toString());
    }

    public <T,R> void setField(T obj, R value, BiConsumer<T, R> setter) {
        setter.accept(obj, value);
    }

    public static void main(String[] args) {
        Person person = new Person();
        Actor actor = new Actor();

        actor.setField(person, "Bob", Person::setName);// (a,b) -> a.setB(b);
        actor.printField(person, Person::getName);
    }
}
  1. How do we interpret the java lambda used for setter?我们如何解释用于设置器的 java lambda? BiConsumer<T, R> functional interface has been used for parameter type for setField and Person::setName was passed as parameter value for the method. BiConsumer<T, R>功能接口已用于setField的参数类型,并且Person::setName作为方法的参数值传递。 My understanding of BiConsumer<T, R> is that its a functional interface used to denote the lambda function with two input parameters and no return parameter ie, (a,b) -> a+b;我对BiConsumer<T, R>的理解是它是一个功能接口,用于表示 lambda function 有两个输入参数,没有返回参数,即 (a,b) -> a+b; where a+b could be any logic(sequence of steps that uses a and b parameters).其中a+b可以是任何逻辑(使用 a 和 b 参数的步骤序列)。 But in the above code example BiConsumer<T, R> denotes the lambda function (personObject,nameValue) -> personObject.set(nameValue);但在上面的代码示例中 BiConsumer<T, R> 表示 lambda function (personObject,nameValue) -> personObject.set(nameValue);
  2. How do we interpret the java lambda used for getter?我们如何解释用于吸气剂的 java lambda? My understanding Function<T,R> = (a) -> return math(a);我的理解Function<T,R> = (a) -> return math(a); Whereas in the above code example Function<T,R> = (personObject) -> return personObject.getName();而在上面的代码示例中Function<T,R> = (personObject) -> return personObject.getName();

Can I get help to understand that above two interpretations please.请帮助我理解以上两种解释。

  1. Exactly, a BiConsumer consumes 2 things.确切地说,一个BiConsumer消费了 2 样东西。 [A] An instance of the Person class, and [B] the value for the name field to set. [A] Person class 的一个实例,以及 [B] 要设置的name字段的值。

  2. Yup.是的。 The input is an instance of Person , the output is the name of that person.输入是Person的一个实例,output 是那个人的名字。

The key realization is that the getter lambda represents the getter itself without an actual person instance associated with it.关键的实现是getter lambda 表示 getter 本身,没有与之关联的实际人员实例。 If you do want that, you can;如果你确实想要,你可以; something like somePersonInstance::getName is a Producer (takes no inputs and provides an output), whereas something like Person::getName is a Function, takes 1 Person instance and provides an output.类似somePersonInstance::getName的东西是Producer (不接受输入并提供输出),而类似Person::getName的东西是 Function,需要 1 个Person实例并提供 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM