「方法引用为已经有名称的方法提供易读的 lambda 表达式。」「它们提供了一种无需执行就可以引用方法的简单方式。」以上引自《Java 8 编程参考官方教程(第 9 版)》,作者:Herbert Schildt
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 |
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. |
构造函数引用与泛型一起使用的时候变得更有用。通过使用泛型工厂方法,可以创建各种类型的对象。
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. |
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); } |
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |