gpt4 book ai didi

java - Java 8 中的实例方法引用类型有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 20:53:47 24 4
gpt4 key购买 nike

所以 Java 8 引入了方法引用和 docs describe the four types .

我的问题是这两种实例类型有什么区别?

  1. Reference to an instance method of a particular object.
  2. Reference to an instance method of an arbitrary object of a particular type.

两者都引用引用,但有什么显着不同?是用来解决它们的类型推断不同吗? (在他们的示例中)一个是闭包而另一个是 lambda 是否重要?是否与方法的参数数量有关?

最佳答案

  1. myString::charAt 将接受一个 int 并返回一个 char,并且可以用于任何工作的 lambda方法。它本质上转换为 index -> myString.charAt(index)

  2. String::length 将采用 String 并返回 int。它本质上转换为 string -> string.length().

  3. String::charAt 将转换为 (string, index) -> string.charAt(index)

关于java - Java 8 中的实例方法引用类型有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22516331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com