方法如何“引用特定类型的任意对象的实例方法”。解决任意对象? [英] How does a method "reference to an instance method of an arbitrary object of a particular type" resolve the arbitrary object?

查看:746
本文介绍了方法如何“引用特定类型的任意对象的实例方法”。解决任意对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

oracle Java 8文档定义了4种方法您可以使用的引用而不是Lambda表达式。我想要理解的是一种方法引用,描述为:引用特定类型的任意对象的实例方法,写为 ContainingType :: methodName

The oracle Java 8 documentation defines 4 types of method references you can use instead of Lambda Expressions. What I am trying to understand is the kind of method reference described as: "Reference to an instance method of an arbitrary object of a particular type " which is written as ContainingType::methodName.

我不确定我是否遗漏了某些东西,但对我而言似乎更像是:
参考第一个功能接口的抽象方法的参数,假设它的类型为 ContainingType 。我试图想出这个'任意对象'是第二个参数的例子,但当然它不能编译。

I am not sure if I am missing something, but to me it seems more like: "Reference to the first parameter of the abstract method of the Functional Interface, assuming it is of type ContainingType". I tried to come up with examples where this 'arbitrary object' is the second parameter, but of course it does not compile.

是否有官方参考如何对象由编译器解决?我的理解是正确的:

Is there an official reference how this object is resolved by the compiler? Am I correct in my understanding that:


  1. 任意对象必须是抽象方法的第一个参数函数接口的签名。

  2. 方法引用的签名必须与功能接口的抽象方法的签名相同,不带第一个参数

  1. The arbitrary object has to be the 1st parameter of the abstract method of the functional interface.
  2. The signature of the method reference must be the same as that of the abstract method of the functional interface, without the first parameter.

所以带抽象方法的函数接口方法(B b,C c,D d)只能传递实例方法引用 x :: methodImpl B :: methodImpl 。我无法传递 C :: methodImpl ,例如,它将是类 C 的实例它的签名 A methodImpl(B b,D d)

So a functional interface with abstract method A method(B b, C c, D d) can only be passed instance method references x::methodImpl or B::methodImpl. There is no way I can pass C::methodImpl for example, where it would be an instance of class C with its signature A methodImpl(B b, D d).

还有其他我遗失的案例,可能是Oracle以这种模棱两可的方式写这个的原因?

Are there any other cases I am missing, which might be the reason why Oracle wrote this in such an ambiguous way?

推荐答案

不,您的理解是正确的。您链接的文档暗示(但没有充分强调)给定一个期望args a1,a2,a3,... 的函数接口,此类型的方法引用是相当于一个lambda,它调用 a1.namedMethod(a2,a3,...)

No, your understanding is correct. The documentation you linked implies (but does not adequately emphasize) that given a functional interface that expects args a1, a2, a3, ..., a method reference of this type is equivalent to a lambda that calls a1.namedMethod(a2, a3, ...).

注意a为了保持一致性,需要这样的具体定义 - 给出带有两个 String 参数的函数接口的链接文档的示例(String s1,String s2 ),您如何确定行为是 s1.doThing(s2)还是 s2.doThing(s1) 否则?

Note that a concrete definition like this is required for consistency's sake - given the example on the linked documentation of a functional interface with two String arguments (String s1, String s2), how would you determine whether the behavior would be s1.doThing(s2) or s2.doThing(s1) otherwise?

您可以在 JLS


如果编译时声明是一个实例方法,那么方法调用表达式的参数(如果有的话)是第二个和后续的形式调用方法的al参数。否则,方法调用表达式的参数是调用方法的形式参数。

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.

这篇关于方法如何“引用特定类型的任意对象的实例方法”。解决任意对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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