特定类型的方法引用
Java约定:如果某个Lambda表达式里只是调用一个实例方法,并且前面参数列表中的第一个参数作为方法的主调, 后面的所有参数都是作为该实例方法的入参时,则就可以使用特定类型的方法引用。格式:
类型::方法名
构造器引用
interface CreateStudent{
Student create(String name, int age);
}
匿名内部类对象
CreateStudent createStudent = new CreateStudent() {
@Override
public Student create(String name, int age) {
return new Student(name,age);
}
};
匿名内部类简化
CreateStudent createStudent1=(name,age)->new Student(name,age);
构造器引用
CreateStudent createStudent2=Student::new;