Java 8中的实例方法引用类型之间有什么区别? [英] What's the difference between instance method reference types in Java 8?
问题描述
因此,Java 8引入了方法引用,文档描述了这四种类型。 / p>
我的问题是两种实例类型之间有什么区别?
- 引用特定对象的实例方法。
- 引用特定类型的任意对象的实例方法。
两者都是指引用,但有什么显着不同?用于解决它们的类型推断是不同的吗?重要的是(在他们的例子中)一个是闭包而另一个是lambda?是否与方法的参数数量有关?
1) myString :: charAt
将采用 int
并返回 char
,并可能用于任何lambda那是这样的。它基本上转换为 index - > myString.charAt(index)
。
2) String :: length
将采取a String
并返回 int
。它基本上翻译为 string - > string.length()
。
我甚至不确定 String :: charAt
将转换为(字符串,索引) - > string.charAt(指数)
。不过,我有点期待它。
So Java 8 introduces method references and the docs describe the four types.
My question is what's the difference between the two instance types?
- Reference to an instance method of a particular object.
- Reference to an instance method of an arbitrary object of a particular type.
Both refer to references but what's significantly different? Is it that the type inference used to resolve them is different? Is it significant that (in their examples) one is a closure and the other is a lambda? Is it something to do with the number of arguments on a method?
1) myString::charAt
would take an int
and return a char
, and might be used for any lambda that works that way. It translates, essentially, to index -> myString.charAt(index)
.
2) String::length
would take a String
and return an int
. It translates, essentially, to string -> string.length()
.
I'm not even sure if String::charAt
would translate to (string, index) -> string.charAt(index)
. I'd kind of expect it to, though.
这篇关于Java 8中的实例方法引用类型之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!