所以 Java 8 引入了方法引用和 docs describe the four types .
我的问题是这两种实例类型有什么区别?
- Reference to an instance method of a particular object.
- Reference to an instance method of an arbitrary object of a particular type.
两者都引用引用,但有什么显着不同?是用来解决它们的类型推断不同吗? (在他们的示例中)一个是闭包而另一个是 lambda 是否重要?是否与方法的参数数量有关?
最佳答案
myString::charAt
将接受一个int
并返回一个char
,并且可以用于任何工作的 lambda方法。它本质上转换为index -> myString.charAt(index)
。String::length
将采用String
并返回int
。它本质上转换为string -> string.length()
.String::charAt
将转换为(string, index) -> string.charAt(index)
。
关于java - Java 8 中的实例方法引用类型有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22516331/