函数引用必须保证所用接口与引用的方法的 形参和返回值类型一致
让左边的函数跟右边的方法具有相同的逻辑,前提条件是两边具有相同的参数列表跟返回类型
- 引用类的静态方法 格式 类名::方法名
例如:
public void method1(){
//1,引用类的静态方法 类名::静态方法名
Consumer<String> c = Student::play;
c.accept("和平精英");
Function<Integer, String> f = String::valueOf ;
String s=f.apply(15);
System.out.println(s);
}
- 引用某个对象的普通方法 对象名::方法名
例如:
public void method2(){
//2,引用某个对象的普通方法 对象名::方法名
Student s = new Student();
Consumer<Integer> c = s::study;
c.accept(18);
//定义具有 System.out.println
Consumer<String> c1 =System.out::println;
c1.accept("发快递费南");
}
- 引用类的构造方法 类名::new
public void method3(){
//3,引用某个类的构造方法 类::new
Supplier<Student> s = Student::new;
Student s1 = s.get();
}
- 引用任意对象的方法 类名::方法名
public void method4(){
//4,引用任意对象的方法 类名::方法名
Student s = new Student();
BiFunction<Student, Integer, String> bf = Student::sleep ;
bf.apply(s, 18);
}