编程语言
首页 > 编程语言> > java-String.concat用作BiFunction

java-String.concat用作BiFunction

作者:互联网

在尝试使用方法引用时,遇到了concat方法可用作BiFunction的情况,据我了解,BiFunction apply方法需要2个输入参数并产生结果.而concat方法采用1个输入参数,并返回具有此值的串联字符串.

样例代码:

public class Test {
  public static void main(String[] args) {
    Test t = new Test();
    String str1 = "Hello";
    String str2 = "Workld";
    System.out.println(t.stringManipulator(str1, str2, String::concat));
    System.out.println(str1);
    System.out.println(str2);
  }
  private String stringManipulator(String inputStr, String inputStr2, BiFunction<String, String, String> function) {
    return function.apply(inputStr, inputStr2);
  }
}

输出量

HelloWorkld
Hello
Workld

有人可以帮我了解这里发生了什么吗?

解决方法:

The method reference String::concat represents an instance method
reference for a target type whose function takes two String arguements
and returns a String. The first arguement will be the receiver of the
concat() method and the second arguement will be passed to the
concat() method.

有关未绑定接收器的更多详细信息,请参考Beginning Java8 book.它已经解释了确切的例子.

标签:method-reference,java-8,java
来源: https://codeday.me/bug/20191026/1935743.html