通过this引用本类的成员方法
// 定义一个富有的函数式接口
@FunctionalInterface
public interface Richable {
// 定义一个想买什么就买什么的方法
void buy();
}
// 通过this引用本类的成员方法
public class Husband {
// 定义一个买房子的方法
public void buyHouse(){
System.out.println("北京二环内买一套四合院!");
}
// 定义一个结婚的方法,参数传递Richable接口
public void marry(Richable r){
r.buy();
}
// 定义一个非常高兴的方法
public void soHappy(){
// 调用结婚的方法,方法的参数Richable是一个函数式接口,传递Lambda表达式
/*marry(()->{
// 使用this.成员方法,调用本类买房子的方法
this.buyHouse();
});*/
/*使用方法引用优化Lambda表达式
this是已经存在的
本类的成员方法buyHouse也是已经存在的
所以我们可以直接使用this引用本类的成员方法buyHouse*/
marry(this::buyHouse);
}
public static void main(String[] args) {
new Husband().soHappy();
}
}
类的构造器(构造方法)引用
// 定义一个创建Person对象的函数式接口
@FunctionalInterface
public interface PersonBuilder {
// 定义一个方法,根据传递的姓名,创建Person对象返回
Person builderPerson(String name);
}
public class Demo {
// 定义一个方法,参数传递姓名和PersonBuilder接口,方法中通过姓名创建Person对象
public static void printName(String name,PersonBuilder pb){
Person person = pb.builderPerson(name);
System.out.println(person.getName());
}
public static void main(String[] args){
// 调用printName方法,方法的参数PersonBuilder接口是一个函数式接口,可以传递Lambda
printName("Xxx",(String name)->{
return new Person(name);
});
/*使用方法引用优化Lambda表达式
构造方法new Person(String name)已知
创建对象已知 new
就可以使用Person引用new创建对象*/
printName("Xxx",Person::new); // 使用Person类的带参构造方法,通过传递的姓名创建对象
}
}
数组的构造器引用
// 定义一个创建数组的函数式接口
@FunctionalInterface
public interface ArrayBuilder {
// 定义一个创建int类型数组的方法,参数传递数组的长度,返回创建好的int类型数组
int[] builderArray(int length);
}
public class Demo {
// 定义一个方法,方法的参数传递创建数组的长度和ArrayBuilder接口,方法内部根据传递的长度使用ArrayBuilder中的方法创建数组并返回
public static int[] createArray(int length, ArrayBuilder ab){
return ab.builderArray(length);
}
public static void main(String[] args) {
// 调用createArray方法,传递数组的长度和Lambda表达式
int[] arr1 = createArray(10,(len)->{
// 根据数组的长度,创建数组并返回
return new int[len];
});
System.out.println(arr1.length); // 10
/*使用方法引用优化Lambda表达式
已知创建的就是int[]数组
数组的长度也是已知的
就可以使用方法引用
int[]引用new,根据参数传递长度来创建数组*/
int[] arr2 = createArray(10,int[]::new);
System.out.println(Arrays.toString(arr2)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(arr2.length); // 10
}
}