方法引用的一些背景
如果你还不知道 Java 构造函数本身就是特殊的方法,那么阅读方法引用的基本示例将对读者有所帮助,通过了解这些内容,可以了解构造函数引用是什么。
方法引用可以引用静态方法和实例方法,两者是通用的。方法引用是函数式接口的实例。虽然 Lambda 表达式允许你动态创建方法实现,但通常情况下,一个方法最终会调用 Lambda 表达式中的另一个方法来完成我们想要完成的工作。更直接的方法是使用方法引用。当你已经有一个方法来实现这个函数式接口时,这是非常有用的。
欢迎学Java和大数据的朋友们加入java架构交流: 855835163
群内提供免费的架构资料还有:Java工程化、高性能及分布式、高性能、深入浅出。高架构。性能调优、Spring,MyBatis,Netty源码分析和大数据等多个知识点高级进阶干货的免费直播讲解 可以进来一起学习交流哦
让我们看一个使用静态方法及实例方法的示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //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(); } } |
译注:以上代码的测试用例如下,因静态方法与实例方法结果相同,仅以静态方法为例。
1 2 3 4 5 6 | 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 ); |
得到与原文举例相同的输出结果:
1 2 3 4 5 6 | "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 |
使用方法引用的主要步骤有:
- 定义一个函数式接口
- 定义一个满足函数式接口抽象方法要求的方法
- 使用对步骤2中定义的 (x :: y ) 方法引用实例化函数式接口的实例。
译注:静态方法的方法引用格式为 类名 :: 方法名
;实例方法的方法引用格式为 对象实例名 :: 方法名
。 - 使用函数式接口实例调用方法:
Instance.AbstractMethod();
这提供了一种创建方法实现的可插拔方式。Lambda 表达式和方法引用为 Java 编程带来了一个功能方面的提升。
构造函数的方法引用
让我们开始详细讨论吧。
构造函数和其他方法一样是方法。对吗?错。它们有点特殊,它们是对象初始化方法。尽管如此,它们仍然是一个方法,没有什么能阻止我们像其他方法引用一样创建构造函数的方法引用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | //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 ; //We really don't gain much from this example //Example #1 Automobile honda = auto.auto( "honda" , "Accord" , ( short ) 2006 ); honda.what(); //Example #1 Automobile bmw = auto.auto( "BMW" , "530i" , ( short ) 2006 ); bmw.what(); System.out.println(); } } |
输出结果
1 2 | This Automobile is a2006 honda Accord. This Automobile is a2006 BMW 530i. |
说明
用户应该清楚的第一件事是这个基本示例没有那么实用。这是一种相当迂回的创建对象实例的方式。实际上,几乎可以肯定,你不会经历所有这些麻烦来创建一个 Automobile 实例,但是为了概念的完整性,还是要提及。
使用构造函数的方法引用的主要步骤有:
- 定义一个只有抽象方法的函数式接口,该方法的返回类型与你打算使用该对象进行构造函数引用的对象相同。
- 创建一个类,该类的构造函数与函数式接口的抽象方法匹配。
- 使用对步骤 #2 中定义的构造函数的方法引用,实例化函数式接口的实例。
译注:构造函数的方法引用格式为 类名 :: new
- 在步骤 #2 中使用构造函数引用实例化类的实例,例如
MyClass x = ConstructorReference.AbstractMethod (x, y, z…)
构造函数引用与泛型一起使用的时候变得更有用。通过使用泛型工厂方法,可以创建各种类型的对象。
让我们看一看。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | //step #1 - Create a funnctional interface. interface FuncInt<Ob, X, Y, Z> { //contains one and only abstract method Ob func(X make, Y model, Z year); } //step #2 - Create a Generic class providing a constructor compatible with FunInt.func()'s definition class Automobile<X, Y, Z> { //Automobile Member Variables private X make; private Y model; private Z year; //Automobile Constructor public Automobile(X make, Y model, Z 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 - Create a Non-Generic class providing a constructor compatible with FunInt.func()'s definition class Plane { //Automobile Member Variables private String make; private String model; private int year; //Plane Constructor public Plane(String make, String model, int year) { this .make = make; this .model = model; this .year = year; //Automatic unboxing } protected void what() { System.out.println( "This Plane is a " + year + " " + make + " " + model + "." ); } } //Step #3 - Class making use of method reference with generics public class ConstrRefGen { //Here is where the magic happens static <Ob, X, Y, Z> Ob factory(FuncInt<Ob, X, Y, Z> obj, X p1, Y p2, Z p3) { return obj.func(p1, p2, p3); } public static void main(String[] args) { System.out.println(); //Example #1 FuncInt<Automobile<String, String, Integer>, String, String, Integer> auto_cons = Automobile<String, String, Integer>:: new ; Automobile<String, String, Integer> honda = factory(auto_cons, "Honda" , "Accord" , 2006 ); honda.what(); //Example #2 FuncInt<Plane, String, String, Integer> plane_cons = Plane:: new ; Plane cessna = factory(plane_cons, "Cessna" , "Skyhawk" , 172 ); cessna.what(); System.out.println(); } } |
输出结果
1 2 | This Automobile is a 2006 Honda Accord. This Plane is a 172 Cessna Skyhawk. |
说明
这里有很多东西需要消化。事实上,如果你以前从未深入研究过泛型,那么这些代码看上去可能相当晦涩。让我们分解一下。
我们做的第一件事是创建一个通用的函数式接口。注意细节。我们有四个泛型类型参数:Ob、X、Y、Z。
- Ob 代表要引用其构造函数的类。
- X,Y,Z 代表该类的构造函数的参数。
如果我们替换泛型方法占位符,抽象方法可能是这样的: SomeClass func (String make, String model, int year)
。注意,由于我们使接口具有了泛型,所以可以指定任何返回类型或我们希望返回的类实例。这释放了构造函数引用的真正潜力。
接下来的两个部分相对简单,我们创建了相同的类,一个泛型类和一个非泛型类,以演示它们与在公共类中定义的工厂方法的互操作性。注意,这些类的构造函数与 FuncInt.func()
的方法签名是兼容的。
进入公共类的文件。这个方法就是奇迹发生的地方。
1 2 3 4 | //Here is where the magic happens static <Ob, X, Y, Z> Ob factory(FuncInt<Ob, X, Y, Z> obj, X p1, Y p2, Z p3) { return obj.func(p1, p2, p3); } |
我们将该方法标记为静态的,所以我们可以不使用 ConstRefGen 实例,毕竟它是一个工厂方法。注意,factory 方法具有与函数式接口相同的泛型类型参数。注意,方法的返回类型是 Ob,它可以是由我们决定的任何类。当然,X、Y、Z是 Ob 中方法的方法参数。请注意,该函数以 FuncInt 的一个实例作为参数(类类型和方法参数作为类型参数),同时也接受 Ob 类型的类作为方法的参数。
在方法体中,它调用方法引用并将在 factory()
中传递的参数提供给它。
我们的第一个任务是创建一个符合 FuncInt<>
的方法引用。
这里我们分别引用 Automobile 类和 Plane 类的构造函数。
我们的下一个任务是创建一个带有方法引用的对象。
为此,我们调用 factory()
并将它需要的构造函数引用以及 factory()
定义的有关构造函数的参数提供给它。factory()
可以灵活地创建对各种方法的构造函数引用,因为它是通用的。因为 Plane 类和 Automobile 类的构造函数匹配 FuncInt.func()
的方法签名,所以它们可作为 FuncInt.func()
的方法引用使用。factory()
通过调用 obj.func(x,y,z)
返回类的一个实例,这是一个构造函数方法引用,当求值时,它将为你提供指定为其参数的类的一个实例。
斟酌这个问题一段时间,会发现它是Java的一个非常有用的补充 ;)
欢迎学Java和大数据的朋友们加入java架构交流: 855835163
群内提供免费的架构资料还有:Java工程化、高性能及分布式、高性能、深入浅出。高架构。性能调优、Spring,MyBatis,Netty源码分析和大数据等多个知识点高级进阶干货的免费直播讲解 可以进来一起学习交流哦