JDK 8 见证了一个特殊特性的出现:构造函数引用和方法引用。在本文中, Adrian D. Finlay 探讨了开发人员如何释放构造函数引用的真正潜力。
方法引用的一些背景
如果你还不知道 Java 构造函数本身就是特殊的方法,那么阅读方法引用的基本示例将对读者有所帮助,通过了解这些内容,可以了解构造函数引用是什么。
「方法引用为已经有名称的方法提供易读的 lambda 表达式。」
「它们提供了一种无需执行就可以引用方法的简单方式。」
以上引自《Java 8 编程参考官方教程(第 9 版)》,作者:Herbert Schildt
译注:该书的第 8 版中文译本名称为:《Java 完全参考手册(第 8 版)》,第 9 版中文译本名称为:《Java 8 编程参考官方教程(第 9 版)》
方法引用可以引用静态方法和实例方法,两者是通用的。方法引用是函数式接口的实例。虽然 Lambda 表达式允许你动态创建方法实现,但通常情况下,一个方法最终会调用 Lambda 表达式中的另一个方法来完成我们想要完成的工作。更直接的方法是使用方法引用。当你已经有一个方法来实现这个函数式接口时,这是非常有用的。
让我们看一个使用静态方法及实例方法的示例。
//step #1 - Create a funnctional interface. interface FuncInt { //contains one and only abstract method String answer(String x, boolean y); } //step #2 - Class providing method(s)that match FuncInt.answer()'s definition. class Answer { static String ans_math_static(String x, Boolean y) { return "\"" + x + "\"" + "\t = \t" + y.toString().toUpperCase(); } String ans_math_inst(String x, Boolean y) { return "\"" + x + "\"" + "\t = \t" + y.toString().toUpperCase(); } }
译注:以上代码的测试用例如下,因静态方法与实例方法结果相同,仅以静态方法为例。
Answer.ans_math_static("9 > 11 ?", false); Answer.ans_math_static("987.6 < 1.1 ?", false); Answer.ans_math_static("1 > 0.9 ?", true); Answer.ans_math_static("T/F: Is Chengdu in Sichuan?", true); Answer.ans_math_static("-1 % 0.2=0 ?", false); Answer.ans_math_static("T/F: Does Dwyne Wade play for the Knicks?", false);
得到与原文举例相同的输出结果:
"9 > 11 ?" = FALSE "987.6 < 1.1 ?" = FALSE "1 > 0.9 ?" = TRUE "T/F: Is Chengdu in Sichuan?" = TRUE "-1 % 0.2=0 ?" = FALSE "T/F: Does Dwyne Wade play for the Knicks?" = FALSE
另请参阅:关于var的所有内容:局部变量类型推断如何清除Java代码冗余
使用方法引用的主要步骤有:
- 定义一个函数式接口
- 定义一个满足函数式接口抽象方法要求的方法
- 使用对步骤2中定义的 (x :: y ) 方法引用实例化函数式接口的实例。
译注:静态方法的方法引用格式为类名 :: 方法名
;实例方法的方法引用格式为对象实例名 :: 方法名
。 - 使用函数式接口实例调用方法:
Instance.AbstractMethod();
这提供了一种创建方法实现的可插拔方式。Lambda 表达式和方法引用为 Java 编程带来了一个功能方面的提升。
另请参阅:你到底有多了解Java的注解?
构造函数的方法引用
让我们开始详细讨论吧。
构造函数和其他方法一样是方法。对吗?错。它们有点特殊,它们是对象初始化方法。尽管如此,它们仍然是一个方法,没有什么能阻止我们像其他方法引用一样创建构造函数的方法引用。
//step #1 - Create a funnctional interface. interface FuncInt { //contains one and only abstract method Automobile auto(String make, String model, short year); } //step #2 - Class providing method(s)that match FuncInt.answer()'s definition. class Automobile { //Trunk Member Variables private String make; private String model; private short year; //Automobile Constructor public Automobile(String make, String model, short year) { this.make = make; this.model = model; this.year = year; } protected void what() { System.out.println("This Automobile is a" + year + " " + make + " " + model + "."); } } //Step #3 - Class making use of method reference public class ConstrRef { static void createInstance() { } public static void main(String[] args) { System.out.println(); //Remember, a Method Reference is an instance of a Functional Interface. Therefore.... FuncInt auto = Automobile::new;//W