Java新特性05 方法引用

一. 定义

方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法。

方法引用是Lambda表达式的一种简写形式;

如果Lambda表达式只是调用一个特定的已经存在的方法,则可以使用方法引用;

如果你觉得Lambda的方法体会很长,影响代码的可读性,则可以用方法引用来解决。

二. 语法

使用 :: 操作符将方法名与对象或类的名字分隔开。

三. 分类

(1)静态方法引用 ContainingClass::staticMethodName

(2)实例方法引用 ContainingObject::instanceMethodName

(3)一个类任意对象的实例方法引用 ContainingType::methodName

(4)构造方法引用 ClassName::new

 Note: Lambda表达式是对匿名内部类的简写,而方法引用是对Lambda表达式的简写

四. 举例

1 package com.test.a;
2 
3 public interface Base {    
4     public void f3();
5 }
View Code

(1)静态方法引用

 1 package com.test.a;
 2 
 3 public class Test {
 4     public static void f3() {
 5         System.out.println("hello");
 6     }
 7 
 8     public static void main(String args[]) {
 9         //1. 匿名内部类的实现方式
10 //        Base base = new Base() {
11 //            @Override
12 //            public void f3() {
13 //                System.out.println("hello");
14 //            }
15 //        };
16 //        base.f3();//print "hello"
17         
18 //        //2.Lambda表达式的实现方式
19 //        Base base2=()->{System.out.println("hello");};
20 //        base2.f3();//print "hello"
21         
22         //3.实例方法引用的实现方式
23         Test test=new Test();
24         Base base3=Test::f3;
25         base3.f3();//print "hello"
26         
27         
28     }
29 }
View Code

(2)实例方法引用

 1 package com.test.a;
 2 
 3 public class Test {
 4     public void f3() {
 5         System.out.println("hello");
 6     }
 7 
 8     public static void main(String args[]) {
 9         //1. 匿名内部类的实现方式
10 //        Base base = new Base() {
11 //            @Override
12 //            public void f3() {
13 //                System.out.println("hello");
14 //            }
15 //        };
16 //        base.f3();//print "hello"
17         
18 //        //2.Lambda表达式的实现方式
19 //        Base base2=()->{System.out.println("hello");};
20 //        base2.f3();//print "hello"
21         
22         //3.实例方法引用的实现方式
23         Test test=new Test();
24         Base base3=test::f3;
25         base3.f3();//print "hello"
26         
27         
28     }
29 }
View Code

Note:这个Lambda内部表达式f3方法已经有一个具体的实现方法了,因此直接引用就可以了。

(3)一个类任意对象的实例方法引用

 1 package com.test.a;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collections;
 5 import java.util.Comparator;
 6 import java.util.List;
 7 
 8 public class Test {
 9     public static void main(String args[]) {
10         List<String> list=new ArrayList<>();
11         list.add("d");
12         list.add("a");
13         list.add("c");
14         list.add("b");
15         
16 //        Collections.sort(list, new Comparator<String>() {
17 //
18 //            @Override
19 //            public int compare(String o1, String o2) {
20 //                // TODO Auto-generated method stub
21 //                return o1.compareTo(o2);
22 //            }
23 //        });
24         
25 //        Collections.sort(list, (o1,o2)->{return o1.compareTo(o2);});
26         
27         //简化Lambda,那么我们必须有一个compareTo方法的具体实现的一个方法,发现String类已经有compareTo的具体实现了,可以直接用
28         Collections.sort(list, String::compareTo);
29         
30         for(String s:list) {
31             System.out.println(s);
32         }
33         
34     }
35 }
View Code

(4)构造方法引用

 1 package com.test.a;
 2 
 3 public class Person {
 4     public String name;
 5     public int age;
 6 
 7     public String getName() {
 8         return name;
 9     }
10 
11     public void setName(String name) {
12         this.name = name;
13     }
14 
15     public int getAge() {
16         return age;
17     }
18 
19     public void setAge(int age) {
20         this.age = age;
21     }
22 }
View Code
 1 package com.test.a;
 2 
 3 import java.util.function.Supplier;
 4 
 5 public class Test {
 6     public static void main(String args[]) {
 7         Supplier<Person> supplier=()->new Person();
 8         Person person=supplier.get();
 9         System.out.println(person);
10         
11         Supplier<Person> supplier2=Person::new;
12         Person person2=supplier2.get();
13         System.out.println(person2);
14     }
15 }
View Code
1 com.test.a.Person@65ab7765
2 com.test.a.Person@7229724f
View Code

 或者自定义:

1 package com.test.a;
2 
3 @FunctionalInterface
4 public interface MyFunction<T> {
5 
6     public abstract T f(int age,String name);
7 }
View Code
 1 public class Test {
 2     
 3     public static void main(String args[]) {
 4         MyFunction<Person> myFunction=Person::new;
 5         Person person=myFunction.f(23, "tom");
 6         System.out.println(person.getName());
 7     }
 8 }
 9 
10 
11 tom
View Code
原文地址:https://www.cnblogs.com/Hermioner/p/9663193.html