使用方法引用而不是多参数 lambda [英] Using method reference instead of multi argument lambda

查看:30
本文介绍了使用方法引用而不是多参数 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对引用特定类型的任意对象的实例方法"背后的概念感到困惑.Oracle documentation 有一个关于此的示例:

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 将具有形式参数列表(String a, String 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 的其他参数将按照出现在 lambda 中的顺序传递给 func?我的意思是而不是 (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 稍后更改,则该方法为仍然使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆