方法引用(Method References)
1、什么是方法引用?
(1)方法引用可用于在不调用方法的情况下引用方法。它将方法视为Lambda表达式。它们只能作为语法糖来减少一些lambda的冗长。
(2)当要传递的Lambda体的操作,已有实现方法,可以使用方法引用。
2、方法引用格式
在方法引用中,将包含方法的对象(或类)放在::运算符之前,将方法的名称放在不带参数的方法之后。
3、方法引用的要求
实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的参数列表和返回值类型保持一致。
4.方法引用的三种主要使用情况:
(1)对象::实例方法名
(2)类::静态方法名
(3)类::实例方法名
@Test
public void test1(){
Person person = new Person("tom",3);
Supplier<String> fun1 = () -> person.getName();
System.out.println(fun1.get());
System.out.println("###############################");
//对象调用非静态方法
Supplier<String> fun2 = person::getName;
System.out.println(fun2.get());
}
@Test
public void test2(){
Function<Double,Long> fun1 = d -> Math.round(d);
System.out.println(fun1.apply(11.2));
System.out.println("###############################");
//类来调用静态方法
Function<Double,Long> fun2 = Math :: round;
System.out.println(fun1.apply(11.2));
}
@Test
public void test3(){
Person person = new Person("tom",3);
Function<Person,String> fun1 = p -> p.getName();
System.out.println(fun1.apply(person));
System.out.println("###############################");
//类调用非静态方法
Function<Person,String> fun2 = Person :: getName;
System.out.println(fun2.apply(person));
System.out.println("###############################");
}
//构造器引用
@Test
public void test4(){
Supplier<Person> sup = () -> new Person();
System.out.println(sup.get());
System.out.println("###############################");
//无参构造器
Supplier<Person> sup1 = Person::new;
System.out.println(sup1.get());
}
@Test
public void test5(){
//前面Integer是参数,Person 是返回值类型,所以后面不用写返回值类型
Function<Integer,Person> f = age -> new Person(age);
Person p = f.apply(10);
System.out.println(p);
System.out.println("###############################");
//一个参数构造器
Function<Integer,Person> f1 = Person::new;
Person p1 = f.apply(10);
System.out.println(p1);
}
@Test
public void test6(){
BiFunction<String,Integer,Person> f = (name, age) -> new Person(name,age);
Person p = f.apply("jim",10);
System.out.println(p);
System.out.println("###############################");
//多个参数构造器
BiFunction<String,Integer,Person> f1 = Person::new;
Person p1 = f1.apply("aka",12);
System.out.println(p1);
}
//数组引用 把数组看出特殊的类
@Test
public void test7(){
Function<Integer,String[]> f1 = length -> new String [length];
String [] arr1 = f1.apply(5);
System.out.println(Arrays.toString(arr1));
System.out.println("###############################");
Function<Integer,String[]> f2 = String[]::new;
String [] arr2 = f1.apply(10);
System.out.println(Arrays.toString(arr2));
}
//自定义bean
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public Person() {
System.out.println("Person()......");
}
public Person(Integer age) {
this.age = age;
System.out.println("Person(Integer age)......"+age);
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}