方法引用
2021/7/3 23:27:32
本文主要是介绍方法引用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、方法引用符
:: 该符号为引用运算符,而它所在的表达式被称为方法引用
推导与省略
-
如果使用 Lambda,那么根据“可推导就是可省略”的原则,无需指定参数类型,也无需指定的重载形式,它们都将被自动推导
-
如果使用方法引用,也是同样可以根据上下文进行推导
-
方法引用是 Lambda 的孪生兄弟
2、引用类方法
引用类方法,其实就是引用类的静态方法
格式:类名::方法名
范例:Integer :: parseInt
注:Lambda 表达式被类方法替代时,它的形参全部传递给静态方法作为参数
代码示例:将 String 类型数字转为 int 类型数字
public interface StringConvert { int convertInt(String s); }
public class Test { public static void main(String[] args) { // lambda方式 usrStringConvert(s -> Integer.parseInt(s)); // 方法引用:引用类方法(静态方法),形参全部传递给静态方法作为参数 usrStringConvert(Integer::parseInt); } public static void usrStringConvert(StringConvert sc) { int num = sc.convertInt("156"); System.out.println("num = " + num); } }
num = 156 num = 156
3、引用构造器
引用构造器,其实就是引用构造方法
格式:类名 :: new
范例:Student :: new
注:Lambda 表达式被构造器替代时,它的形参全部传递给构造器作为参数
代码示例:创建学生
public class Student { String name; int age; // 省略无参|有参构造方法、get|set方法、toString方法 }
public interface StudentBuilder { Student builder(String name, int age); }
public class Test { public static void main(String[] args) { // lambda方式 usrStudentBuilder((name, age) -> new Student(name, age)); // 方法引用:引用构造器(构造方法),形参全部传递给构造器作为参数 usrStudentBuilder(Student::new); } public static void usrStudentBuilder(StudentBuilder sb) { Student student = sb.builder("张三", 23); System.out.println("student = " + student); } }
student = Student{name='张三', age=23} student = Student{name='张三', age=23}
4、引用类的实例方法
引用类的实例方法,其实就是引用类中的成员方法
格式:类名 :: 成员方法
范例:String :: substring
注:Lambda 表达式被类的实例方法替代时,第一个参数作为方法调用者,后面其余参数全部传递给该方法作为参数
代码示例:截取字符串,返回一个子串
public interface StringMethod { String mySubString(String string, int begin, int end); }
public class Test { public static void main(String[] args) { // lambda方式 usrStringMethod((string, begin, end) -> string.substring(begin, end)); // 方法引用:引用类的实例方法(成员方法),第一个参数作为方法调用者,后面其余参数全部传递给该方法作为参数 usrStringMethod(String::substring); } public static void usrStringMethod(StringMethod sm) { String string = sm.mySubString("HelloWord!", 3, 6); System.out.println("string = " + string); } }
string = loW string = loW
5、引用对象的实例方法
引用对象的实例方法,其实就引用类中的成员方法
格式:对象::成员方法
范例:"HelloWorld" :: toUpperCase
注:Lambda 表达式被对象的实例方法替代时,它的形参全部传递给该方法作为参数
代码示例
public interface Printer { void printUpper(String s); }
public class PrintString { void printUpperCase(String s) { String string = s.toUpperCase(); System.out.println("string = " + string); } }
public class Test { public static void main(String[] args) { // lambda方式 usrPrinter(s -> System.out.println(s.toUpperCase())); // 方法引用:引用类对象的实例方法(成员方法),形参全部传递给该方法作为参数 PrintString ps = new PrintString(); usrPrinter(ps::printUpperCase); } public static void usrPrinter(Printer p) { p.printUpper("HelloWord!"); } }
HELLOWORD! string = HELLOWORD!
这篇关于方法引用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求