目录
方法引用
1.应用场景
方法引用 来自官方的说明You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name. 你使用lambda 表达式创建匿名方法。 但是,有时 lambda 表达式除了调用现有方法外什么也不做。 在这种情况下,通常更容易按名称引用现有方法。 方法引用使你可以执行此操作;它们是紧凑的,对于已经具有名称的方法lambda 表达式更易于阅读。
示例
package com . wq . funcational ; public interface Actor { /** * 演员表演节目 * @param item */ void perform ( String item ); } package com . wq . funcational ; public class ActorTest { public static void main ( String [] args ) { Actor actor = item -> System . out . println ( item ); actor . perform ( " 跳舞 " ); } }
分析
上面的示例中,Lambda 表达式的作用就是调用 System.out 中的 println(String msg) 方法,这个方法已经有具体的实现,如果能够直接引用这个方法,那么代码将变得更为简洁。2.方法引用符
双冒号 :: 为方法引用符,而它所在的表达式被称为方法引用。如果 Lambda 表达式赋值的方法已经在某个类中有具体的实现,那么则可以通过双冒号来引用该方法作为Lambda 表达式的替代者。示例
public class ActorTest { public static void main ( String [] args ) { Actor actor = System . out :: println ; actor . perform ( " 跳舞 " ); } }
解释说明
Actor 接口中的 void perform(String item) 方法在实现时用的 System.out 中的 public void println(String x) 方法。 Lambda 表达式可以根据实现的接口方法推导省略,方法引用也可以根据实 现的接口方法进行推导省略。 void perform(String item) 方法中带有一个字符串类型的参数,public void println(String x) 方法来实现时就可以接收这个字符串参数。 方法引用与Lambda 表达式一样,只能应用于函数式接口。方法有静态方法、成员方法和构造方法之 分,方法引用因此也分为静态方法引用、成员方法引用和构造方法引用3. 静态方法引用
语法
类名 :: 方法名
示例
package com . wq . _static ; public interface Calculator { int calculate ( int a , int b ); } package com . wq . _static ; public class MathUtil { public static int add ( int a , int b ){ return a + b ; } public static int minus ( int a , int b ){ return a - b ; } public static int multiply ( int a , int b ){ return a * b ; } public static int divided ( int a , int b ){ return a / b ; } } package com . wq . _static ; public class CalculatorTest { public static void main ( String [] args ) { Calculator c = MathUtil :: minus ; int result = c . calculate ( 1 , 10 ); System . out . println ( result ); Calculator c1 = MathUtil :: multiply ; int result1 = c1 . calculate ( 1 , 10 ); System . out . println ( result1 ); } }
4. 成员方法引用
语法
对象名 :: 方法名
示例
package com . wq . _static . member ; public interface Printable { void print ( String msg ); } package com . wq . _static . member ; public class Printer { public void print ( String msg ){ System . out . println ( msg ); } } package com . wq . _static . member ; public class Computer { private Printer printer ; public Computer ( Printer printer ) { this . printer = printer ; } public void print ( String msg ){ Printable printable = printer :: print ; printable . print ( msg ); } } package com . wq . _static . member ; public class ComputerTest { public static void main ( String [] args ) { Computer c = new Computer ( new Printer ()); c . print ( "This is method reference" ); } }注意:如果函数式接口的抽象方法中只有一个引用数据类型的参数,且实现过程只需要调用该类型中定义的成员方法,那么可以使用静态引用的方式直接引用该成员方法
示例
package com . wq . _static . member . _static ; public class Person { public void sing (){ System . out . println ( " 唱歌 " ); } public void dance (){ System . out . println ( " 跳舞 " ); } } package com . wq . _static . member . _static ; public interface Actor { void perform ( Person p ); } package com . wq . _static . member . _static ; public class ActorTest { public static void main ( String [] args ) { Actor a = Person :: dance ; a . perform ( new Person ()); Actor actor = Person :: sing ; actor . perform ( new Person ()); } }
5. this 引用成员方法
语法
this :: 方法名
示例
package com . wq . _static . member . _this ; public interface Camera { void takePhoto ( String name ); } package com . wq . _static . member . _this ; public class Person { public void takePhoto ( String name ){ System . out . println ( " 给 " + name + " 拍照 " ); } public void travel ( String name ){ Camera c = this :: takePhoto ; c . takePhoto ( name ); } } package com . wq . _static . member . _this ; public class PersonTest { public static void main ( String [] args ) { Person p = new Person (); p . travel ( " 金字塔 " ); } }
6. super 引用父类成员方法
语法
super :: 方法名
示例
package com . wq . _static . member . _super ; public interface Customer { /** * 交流业务 */ void communicateBusyness (); } package com . wq . _static . member . _super ; public class SoftEngineer { public void analysisBusyness (){ System . out . println ( " 分析业务 " ); } } package com . wq . _static . member . _super ; public class JavaProgrammer extends SoftEngineer { public void communicateWithCustomer (){ Customer c = super :: analysisBusyness ; c . communicateBusyness (); } } package com . wq . _static . member . _super ; public class JavaProgrammerTest { public static void main ( String [] args ) { JavaProgrammer programmer = new JavaProgrammer (); programmer . communicateWithCustomer (); } }
7. 构造方法引用
语法
类名 :: new
示例
package com . wq . _static . constructor ; public class Student { private String name ; private String sex ; public Student ( String name , String sex ) { this . name = name ; this . sex = sex ; } @Override public String toString () { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + '}' ; } } package com . wq . _static . constructor ; public interface StudentBuilder { Student build ( String name , String sex ); } package com . wq . _static . constructor ; public class StudentBuilderTest { public static void main ( String [] args ) { StudentBuilder builder = Student :: new ; Student stu = builder . build ( " 张三 " , " 男 " ); System . out . println ( stu ); } }标签:Java,String,package,void,53,static,wq,public,SE From: https://blog.csdn.net/m0_63181360/article/details/139152534