简体   繁体   English

Java的方法参考

[英]Method Reference of Java

According to Oracle Documentation , the String::compareToIgnoreCase is also a valid method reference, my question is that compareToIgnoreCase is not a static method, in other words, compareToIgnoreCase must be attached to a specific String instance.根据Oracle DocumentationString::compareToIgnoreCase也是一个有效的方法参考,我的问题是compareToIgnoreCase不是 static 方法,换句话说, compareToIgnoreCase必须附加到特定的String实例。 So how does JDK know which instance of String I refer when I use String::compareToIgnoreCase ?那么当我使用String::compareToIgnoreCase时,JDK 如何知道我引用了哪个String实例?

Consider the following example using toUpperCase which is also an instance method.考虑以下使用toUpperCase的示例,它也是一个实例方法。

It works in this case because the Stream item that is being handled is of the same type as the class of the method being invoked.它在这种情况下有效,因为正在处理的 Stream 项目与正在调用的方法的 class 的类型相同。 So the item actually invokes the method directly.所以item实际上直接调用了方法。

So for因此对于

Stream.of("abcde").map(String::toUpperCase).forEach(System.out::println);

the String::toUpperCase call will be the same as "abcde".toUpperCase() String::toUpperCase调用将与"abcde".toUpperCase()相同

If you did something like this:如果你做了这样的事情:

Stream.of("abcde").map(OtherClass::toUpperCase).forEach(System.out::println);

"abcde" is not a type of OtherClass so the OtherClass would need to look like the following for the stream to work. “abcde”不是OtherClass的一种类型,因此OtherClass需要如下所示,stream 才能工作。

class OtherClass {
    public static String toUpperCase(String s) {
       return s.toUpperCase();
    }
} 

String::compareToIgnoreCase is not used such as str1.compareToIgnoreCase(str2) would be.不使用String::compareToIgnoreCase ,例如str1.compareToIgnoreCase(str2) It actually is used as a comparator.它实际上用作比较器。 Eg you could compare it to例如,您可以将其与

Arrays.sort(someIntegerArray, Collections.reverseOrder()) 

but in this case it would be但在这种情况下

Arrays.sort(someStringArray, String::compareToIgnoreCase) 

It is like there is an additional parameter, the actual instance, involved.就像有一个额外的参数,实际实例,涉及。

Example for String::compareToIgnoreCase : String::compareToIgnoreCase的示例:

ToIntBiFunction<String, String> func = String::compareToIgnoreCase;
int result = func.applyAsInt("ab", "cd");    // some negative number expected

We get a ToIntBiFunction - a two parameter function returning int - since the result is an int , the first parameter correspond to this of compareToIgnoreCase and the second function parameter is the parameter passed to compareToIgnoreCase .我们得到一个ToIntBiFunction - 一个返回int的两个参数 function - 因为结果是一个int ,所以第一个参数对应于compareToIgnoreCasethis而第二个 function 参数是传递给compareToIgnoreCase的参数。


maybe a bit easier:也许更容易一些:

ToIntFunction<String> f = String::length;  // function accepting String, returning int
int length = f.applyAsInt("abc");          // 3

length does not accept any parameter, but the first argument of the function is used as the instance length is called on length不接受任何参数,但是使用 function 的第一个参数作为实例length被调用


The examples above are very abstract, just to show the types involved.上面的例子非常抽象,只是为了展示所涉及的类型。 The functions are mostly used directly in some method call, no need to use an intermediate variable函数大多直接在一些方法调用中使用,不需要使用中间变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM