Java unil BiFunction中的方法参考

jmt*_*jmt 4 java java-8 method-reference

我有问题在java(util)函数中传递方法引用作为参数.

我有两个功能

Function<Value, Output> f1 = (val) -> {
    Output o = new Output();
    o.setAAA(val);
    return o;
};

Function<Value, Output> f2 = (val) -> {
    Output o = new Output();
    o.setBBB(val);
    return o;
};
Run Code Online (Sandbox Code Playgroud)

我想将它们合并为一个应该看起来像的函数

BiFunction<MethodRefrence, Value, Output> f3 = (ref, val) -> {
    Output o = new Output();
    Output."use method based on method reference"(val);
    return o;
};
Run Code Online (Sandbox Code Playgroud)

我想用这个函数

f3.apply(Output::AAA, number);
Run Code Online (Sandbox Code Playgroud)

可能吗 ?我无法弄清楚正确的语法,如何制作这样的功能.

Hol*_*ger 9

看起来你想要一个像这样的功能

BiFunction<BiConsumer<Output,Value>, Value, Output> f = (func, val) -> {
    Output o = new Output();
    func.accept(o, val);
    return o;
};
Run Code Online (Sandbox Code Playgroud)

你可以调用它

f.apply(Output::setAAA, val);
f.apply(Output::setBBB, val);
Run Code Online (Sandbox Code Playgroud)