1,什么是方法引用?
我们可以这样理解:
①方法
②引用
方法就是我们定义的函数方法,引用就是把已经存在的方法拿来用,把它当做函数式接口抽象方法的方法体。
我们在mybatis—plus中会使用大量的方法引用,所以说这个知识点还是很重要的。
2,方法引用分类
(1)引用静态方法
//使用方法
格式:类名::静态方法名
范例:Integer::parseInt
举例:集合中有以下数字"1","2","3","4","5",要求把它们都变成int类型
public class demo1{
public static void main(String[] args){
//创建集合
ArrayList<String> list = new ArrayList<>();
Collection.addAll(list,"1","2","3","4","5");
list.stream().map(Integer::parseInt);//方法引用
}
}
(2)引用成员方法
格式:对象::成员方法
1,其他类:其他对象::方法名
2,本类:this::方法名
3,父类:super::方法名
/*
举例
定义一个类(PrintString),里面定义一个方法
public void printUpper(String s):把字符串参数变成大写的数据,然后在控制台输出
定义一个接口(Printer),里面定义一个抽象方法
void printUpperCase(String s)
定义一个测试类(PrinterDemo),在测试类中提供两个方法
①、一个方法是:usePrinter(Printer p)
②、一个方法是主方法,在主方法中调用usePrinter方法
*/
class PrintString{
//吧字符串参数变成大写的数据,然后再控制台输出
public void printUpper(String s){
String result = s.toUpperCase();
System.out.println(result);
}
}
interface Printer{
void printUpperCase(String s);
}
public class PrinterDemo{
public static void main(String[] args) {
//在主方法中调用usePrinter方法
// usePrinter((String s) ->{
// //String result = s.toUpperCase();
// //System.out.println(result);
//
// //System.out.println(s.toUpperCase());
// });
//Lambda
usePrinter(s -> System.out.println(s.toUpperCase()));
//引用对象的实例方法
PrintString ps = new PrintString();
usePrinter(ps::printUpper);
//Lambda表达式被对象的实例方法替代的时候,它的形式参数全部传递给该方法作为参数
//Lambda表达式里面的形式参数s需要全部传递给printUpper方法作为参数
}
private static void usePrinter(Printer p){
p.printUpperCase("HelloWorld");
}
}
(3)引用构造方法
格式:类名::new
举例:
定义一个集合"zhangsan,23","lisi,24","wangwu,25",把他们转化为Student类
public class Student{
String name;
int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
public Student(String s){
this.name = s.split(",")[0];
this.age = Integer.parseInt(s.split(",")[1]);
}
public String toString(){
return this.name+this.age;
}
}//定义student类
public class demo{
public static void main(String[] args){
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"zhangsan,23","lisi,24","wangwu,25");
list.stream().map(Student::new).forEach(s->System.out.println(s));//引用构造方法
}
}
(4)使用类名引用成员方法
格式:类名::成员方法
举例:
将集合中的字母"a","b","c","d","e",转化为大写。
public class demo{
public static void main(String[] args){
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"a","b","c","d","e");
list.stream().map(String::toUpperCase).forEach(System.out::println);//使用类名引用成员方法
}
}
(5) 引用数组构造方法
格式:类型[]::new
举例:将集合中的元素1,2,3,4,5;放在数组中
public class demo9
{
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list,1,2,3,4,5);
Integer[] arr = list.stream().toArray(new IntFunction<Integer[]>()
{
@Override
public Integer[] apply(int value)
{
return new Integer[value];
}
});//抽象方法
System.out.println(Arrays.toString(arr));
#1,2,3,4,5;
Integer[] arr2 = list.stream().toArray(Integer[]::new);//方法引用
System.out.println(Arrays.toString(arr2));
#1,2,3,4,5;
}
}
总结
方法引用省去了lambda,将接口方法实现的内容封装到具体方法,将方法作为接口实现,实际也是实现且创建一个接口对象。
只是更加抽象,只能看到接口方法内部实现(调用的这个方法就是具体实现)。
采用方法引用可以更近一步的简化代码,有时候这种简化让代码看上去更加的直观。