Java教程

方法引用

本文主要是介绍方法引用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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!
这篇关于方法引用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!