如何在java8中定义没有args或返回vaule的函数?

时间:2018-05-19 14:05:18

标签: java lambda

如何定义没有args或返回vaule的函数?

Class Foo {
    public void setBar(String bar){...};
    public String getBar(){...};
}

? getter = Foo::getBar;
? setter = Foo::setBar;

1 个答案:

答案 0 :(得分:4)

您遗失的内容是FunctionBiConsumer

// Equivalent to: Function<Foo, String> getter = foo -> foo.getBar();
Function<Foo, String> getter = Foo::getBar

// Equivalent to: BiConsumer<Foo, String> setter = (foo, str) -> foo.setBar(str);
BiConsumer<Foo, String> setter = Foo::setBar