使用方法引用而不是多参数lambda [英] Using method reference instead of multi argument lambda
问题描述
我对引用特定类型的任意对象的实例方法背后的概念感到困惑。 Oracle 文档中有一个示例:
I'm confused about concept behind "Reference to an Instance Method of an Arbitrary Object of a Particular Type". Oracle documentation has an example about this:
String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
我在这种方法参考中看到的大多数例子都是这样的:如果lambda就像: x - > x.func()
然后你可以像 ClassOfX :: func
那样编写它。文档中的示例说:
Most of the examples I have seen for this kind of method reference is like this: If lambda is like: x -> x.func()
then you can write it like ClassOfX::func
. The example in documentation says:
方法引用的等效lambda表达式
String :: compareToIgnoreCase将具有形式参数列表
(字符串a,字符串b),其中a和b是用于更好
的任意名称描述此示例。方法引用将调用方法
a.compareToIgnoreCase(b)。
The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).
问题是:对于任何两个参数lambda喜欢(a,b) - > a.func(b)
func
方法必须是第一个参数的实例方法,lambda的第二个参数将作为参数传递给该方法?如果我们有多个参数lambda然后 func
方法必须是lambda的第一个参数的实例方法,而lambda的其他参数将被传递给 func
按照lambda中出现的顺序?我的意思是代替(a,b,c) - > a.func(b,c)
我们可以写 ClassOfA :: func
The question is: for any two argument lambda like (a, b) -> a.func(b)
the func
method must be instance method of first argument and second argument of lambda will be passed as an argument to that method? If we have multiple argument lambda then func
method must be instance method of first argument of lambda and other arguments of lambda will be passed to func
in the order the appear in lambda? I mean instead of (a, b, c) -> a.func(b, c)
we can write ClassOfA::func
对不起我的英语。我希望我能明白这个问题。
I'm sorry for my English. I hope I made the problem clear.
推荐答案
SomeClass :: func
可能意味着两件事,取决于 func
是静态方法还是实例方法。
SomeClass::func
can mean two things, depending on whether func
is a static method or an instance method.
(1)如果 func
是一个静态方法,然后 SomeClass :: func
是一个lambda,它只传递方法的所有参数:
(1) If func
is a static method, then SomeClass::func
is a lambda that just passes all the arguments to the method:
(a, b, c) -> SomeClass.func(a, b, c);
(2)如果 func
是一个实例方法,然后 SomeClass :: func
是一个lambda,它使用第一个参数作为实例,如你所愿:
(2) If func
is an instance method, then SomeClass::func
is a lambda that uses the first argument as the instance, as you thought:
(a, b, c) -> a.func(b, c);
其中 a
的类型为 SomeClass
。
编辑: Sotirios的答案显示了一种不同类型的方法参考: example :: method
其中 example
是一个引用变量(而不是类名)。这意味着与
Sotirios' answer demonstrates yet a different type of method reference: example::method
where example
is a reference variable (instead of a class name). This means the same as
(a, b) -> example.method(a, b);
或者更准确
(a, b) -> __someFinalTemporary.method(a, b);
其中 __ someFinalTemporary
已分配给 example
在评估方法引用的位置,以便如果 example
稍后更改,仍然使用较早的值调用该方法示例
。
where __someFinalTemporary
is assigned to example
at the point where the method reference is evaluated, so that if example
changes later, the method is still called using the earlier value of example
.
[第四种是 SomeClass :: new
将参数传递给构造函数。我认为这就是全部。]
[The fourth kind is SomeClass::new
which passes the arguments to a constructor. I think that's all of them.]
这篇关于使用方法引用而不是多参数lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!