溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java8中的方法引用與構(gòu)造器引用

發(fā)布時(shí)間:2020-06-20 21:34:12 來源:網(wǎng)絡(luò) 閱讀:465 作者:孤魂1996 欄目:編程語言

java8中的方法引用與構(gòu)造器引用

方法引用:若Lambda體中的內(nèi)容有方法已經(jīng)實(shí)現(xiàn)了,我們可以使用“方法引用”

主要的三種語法格式:

  1. 對象::實(shí)例名
  2. 類::靜態(tài)方法名
  3. 類::實(shí)例方法名

注意:

  1. Lmabda體中調(diào)用方法的參數(shù)列表與返回值類型要與函數(shù)式接口中抽象方法的函數(shù)列表和返回值類型保持一致
  2. 若Lambda參數(shù)列表中的第一參數(shù)是 實(shí)例方法的調(diào)用者,而第二個(gè)參數(shù)是 實(shí)例方法的參數(shù)時(shí),可以使用ClassName::method

    public class Employee {
    
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        super();
    }
    
    public  Employee(int age){
        this.age = age;
    }
    
    public Employee(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public double getSalary() {
        return salary;
    }
    
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
    }
    //對象::實(shí)例方法名
    @Test
    public void test1(){
        PrintStream ps1 = System.out;
        Consumer<String> con = (x) -> ps1.println(x);
    
        PrintStream ps = System.out;
        Consumer<String> con1 = ps::println;
    
        Consumer<String> con2 = System.out::println;
        con2.accept("abcdef");
    }
    
    @Test
    public void test2(){
        Employee employee = new Employee();
        Supplier<String> sup = () -> employee.getName();
        String str = sup.get();
        System.out.println(str);
    
        Supplier<String> sup2 = employee::getName;
        String str2 = sup2.get();
        System.out.println(str2);
    
    }
    //類:靜態(tài)方法名
    @Test
    public void test3(){
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
    
        Comparator<Integer> com1 = Integer::compare;
    }
    構(gòu)造器引用

    格式:ClassName::new
    注意:需要調(diào)用的構(gòu)造器的參數(shù)列表要與函數(shù)式接口中抽象方法的參數(shù)列表保持一致

    public class Employee {
    
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        super();
    }
    
    public  Employee(int age){
        this.age = age;
    }
    
    public Employee(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public double getSalary() {
        return salary;
    }
    
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
    }
    //構(gòu)造器引用
    @Test
    public void test5(){
        Supplier<Employee> sup = () -> new Employee();
    
        //構(gòu)造器引用方式
        Supplier<Employee> sup2 = Employee::new;
        Employee employee = sup2.get();
        System.out.println(employee);
    }
    
    @Test
    public void test6(){
        Function<Integer, Employee> fun = (x) -> new Employee(x);
    
        Function<Integer, Employee> fun2 = Employee::new;
        Employee emp = fun2.apply(101);
        System.out.println(emp);
    
    }
    數(shù)組引用

    格式:Type[ ]::new;

    //數(shù)組引用
    @Test
    public void test7(){
        Function<Integer, String[]> fun = (x) -> new String[x];
        String[] strs = fun.apply(10);
        System.out.println(strs.length);
    
        Function<Integer, String[]> fun2 = String[]::new;
        String[] strs2 = fun2.apply(20);
        System.out.println(strs2.length);
    }
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI