gpt4 book ai didi

java - 当有多个参数时,方法引用如何工作

转载 作者:行者123 更新时间:2023-11-29 06:26:19 24 4
gpt4 key购买 nike

编译器如何确保以下语句的等效 lambda

BinaryOperator<String> concatOperator = String::concat; 

BinaryOperator<String> concatOperator = (resultString, inputString) -> resultString.concat(inputString);

不是

BinaryOperator<String> concatOperator = (resultString, inputString) -> inputString.concat(resultString);

最佳答案

此行为在 JLS 中有详细记录

15.13.3. Run-Time Evaluation of Method References

If the compile-time declaration is an instance method, then the target reference is the first formal parameter of the invocation method. Otherwise, there is no target reference.

If the compile-time declaration is an instance method, then the arguments to the method invocation expression (if any) are the second and subsequent formal parameters of the invocation method. Otherwise, the arguments to the method invocation expression are the formal parameters of the invocation method.

这似乎是合理且直观的。如果你采用一个带有元数 n (n > 2) 的方法,很明显目标引用应该是第一个参数,而不是最后一个,而不是中间。

关于java - 当有多个参数时,方法引用如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com