方法引用的形式
方法引用的标准形式是:类名::方法名。(注意:只需要写方法名,不需要写括号)
有以下四种形式的方法引用:
类型 示例
引用静态方法 ContainingClass::staticMethodName
引用某个对象的实例方法 containingObject::instanceMethodName
引用某个类型的任意对象的实例方法 ContainingType::methodName
引用构造方法 ClassName::new
下面我们通过一个小Demo来分别学习这几种形式的方法引用:
Student 类:
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
| public class Student { private String name; private int score;
public Student(String name, int score) { this.name = name; this.score = score; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getScore() { return score; }
public void setScore(int score) { this.score = score; }
public static int coompareStudentByScore(Student student1,Student student2){ return student1.getScore() - student2.getScore(); }
public static int coompareStudentByName(Student student1,Student student2){ return student1.getName().compareToIgnoreCase(student2.getName()); }
public int compareScore(Student student){ return this.score-student.getScore(); }
}
|
StudentComparator 比较器:
1 2 3 4 5 6 7 8 9 10 11
| public class StudentComparator {
public int coompareStudentByScore(Student student1,Student student2){ return student1.getScore() - student2.getScore(); }
public int coompareStudentByName(Student student1,Student student2){ return student1.getName().compareToIgnoreCase(student2.getName()); }
}
|
测试类:MethodReferenceTest
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
| * Created by CeaserWang on 2017/1/15. */ public class MethodReferenceTest {
public String toString(Supplier<String> supplier){ return supplier.get()+"test"; }
public String toString2(String str, Function<String,String> function){ return function.apply(str); }
public static void main(String[] args) { Student student1 = new Student("zhangsan",20); Student student2 = new Student("lisi",90); Student student3 = new Student("wangwu",50); Student student4 = new Student("zhaoliu",60);
List<Student> students = Arrays.asList(student1,student2,student3,student4); //lambda形式 students.sort((studentparam1,studentparam2) -> Student.coompareStudentByName(studentparam1,studentparam2)); students.forEach(student -> System.out.println(student.getName()));*/ // 引用静态方法 /* students.sort( Student::coompareStudentByName); students.forEach(student -> System.out.println(student.getName()));
System.out.println("----------------------------------------------");
//引用某个对象的实例方法 StudentComparator studentComparator = new StudentComparator(); students.sort(studentComparator::coompareStudentByScore); students.forEach(student -> System.out.println(student.getScore()));
System.out.println("----------------------------------------------");
//引用某个类型的任意对象的实例方法 students.sort(Student::compareScore); students.forEach(student -> System.out.println(student.getScore())); MethodReferenceTest methodReferenceTest = new MethodReferenceTest(); //引用构造方法 System.out.println( methodReferenceTest.toString(String::new)); System.out.println( methodReferenceTest.toString2("hello",String::new)); } }
|