方法引用符
方法应用符号
-
::
该符号为引用运算符,而它所在的表达式被成为方法引用
代码例子
- Lambda表达式:
usePrintable(s->System.out.println(s));
分析:拿到参数s之后通过Lambda表达式,传递给System.out.println
方法去处理 - 方法引用:
usePrintable(System.out::println);
分析:之间使用System.out
中的println
方法来取消Lambda,代码更加简洁
推到与省略
- 如果使用Lambda,根据“可推导就是可省略”原则,无需指定参数类型,无需指定重载形式,它们都将被自动推导
- 如果使用方法引用,也是同样可以根据上下文进行推导
- 方法引用时Lambda的孪生兄弟
usePrintable(i -> System.out.println(i));
usePrintable(System.out::println);
private static void usePrintable(Printable p){
p.printInt(666);
}
Lambda表达式支持的方法引用
常见的引用方式
- 引用类方法
- 引用对象的实例方法
- 引用类的实例方法
- 引用构造器
引用类方法
引用类方法,其实就是引用类的静态方法
- 格式:
类名::静态方法
- 范例:
Integer::parseInt
Integer类的方法:public static int parseInt(String s)
将此String转换为Int类型数据
练习1
//Converter接口
public interface Converter {
int convert(String s);
}
//ConverterDemo
public class ConverterDemo {
public static void main(String[] args) {
//在主方法中调用useConverter方法
useConverter(s -> Integer.parseInt(s));
//引用类方法
useConverter(Integer::parseInt);
//Lambda表达式被类方法替代的时候,形式参数全部传递给静态方法作为参数
}
private static void useConverter(Converter c){
int number=c.convert("666");
System.out.println(number);
}
}
引用对象的实例方法
引用对象的实例方法,其实就是引用类中的成员方法
- 格式:
对象::成员方法
- 范例:
HelloWorld::toUpperCase
String类中的方法:public String toUpperCase()
将此String所以的字符转换为大写
练习
- 定义一个类(PrintString),里面定义一个方法
public void printUpper(String s)
把字符串参数变成大写的数据,然后在cmd输出 - 定义一个接口(Printer),里面定义一个抽象方法
void printUpperCase(String s)
- 定义一个测试类(PrintDemo),在测试类中提供两个方法
一个方法是:usePrinter(Printer p)
主方法:在其中调用usePrint方法
//Printer
public interface Printer {
void printUpperCase(String s);
}
//PrinterDemo
public class PrinterDemo {
public static void main(String[] args) {
//在主方法中调用usePrinter方法
usePrinter(s -> System.out.println(s.toUpperCase()));
//应用对象实例方法
PrintString ps=new PrintString();
usePrinter(ps::printUpper);
//Lambda表达式被对象实例方法替代时,形式参数全部传递给该方法作为参数
}
private static void usePrinter(Printer p){
p.printUpperCase("HelloWorld");
}
}
//PrintString
public class PrintString {
public void printUpper(String s){
String result=s.toUpperCase();
System.out.println(result);
}
}
引用类的实例方法
引用类的实例方法,其实就是引用类中的成员方法
- 格式:
类名::成员方法
- 范例:
String::substring
String类中的方法:
从beginIndex开始到endIndex结束,截取字符串。返回一个子串。
子串长度endIndex-beginIndex
练习
- 定义一个接口(MyString),里面定义一个抽象方法
String mySubString(String s,int x,int y)
- 定义一个测试类(MyStringDemo),在测试类中提供两个方法
一个方法是:useMyString(MyString my)
主方法:在其中调用useMyString方法
//MyString
public interface MyString {
String mySubString(String s,int x,int y);
}
//MyStringDemo
public class MyStringDemo {
public static void main(String[] args) {
//在主方法中调用useMyString方法
useMyString((s,x,y)-> s.substring(x,y));
//引用类的实例方法
/*
Lambda表达式被类的实例方法替代的时候
第一个参数作为调用者
后面的参数全部传递给该方法作为参数
*/
}
private static void useMyString(MyString my){
String s=my.mySubString("HelloWorld",2,5);
System.out.println(s);
}
}
引用构造器
引用构造器,其实就是引用构造方法
- 格式:
类名::new
- 范例:
Student::new
练习
- 定义一个类(Student),里面带有两个成员变量(name,age)
- 定义一个接口(StudentBuilder),里面定义一个抽象方法
Student build(String name,int age)
- 定义一个测试了(StudentDemo),在测试类中提供两个方法
一个方法:useStudentBuilder(StudentBuilder s)
主方法:在其中调用useStudentBuilder方法
//StudentBuilder
public interface StudentBuilder {
Student build(String name,int age);
}
//StudentDemo
public class StudentDemo {
public static void main(String[] args) {
//在主方法中调用useStudentBuilder方法
useStudentBuilder((name,age)->new Student(name,age));
/*useStudentBuilder((String name,int age)->{
Student s=new Student(name,age);
return s;
});*/
//引用构造器
useStudentBuilder(Student::new);
//Lambda表达式被构造器替代的时候,它的形式参数全部传递给构造器作为参数
}
private static void useStudentBuilder(StudentBuilder sb){
Student s=sb.build("Tom",30);
System.out.println(s.getName()+","+s.getAge());
}
}