详解Java方法引用

1.通过对象名引用成员方法
  • 使用前提是对象名是已经存在的,成员方法也是已经存在的

  • 就可以使用对象名来引用成员方法

package com.MethodReference.ObjectMethodReference;
/*
   定义一个打印的函数式接口
 */
@FunctionalInterface
public interface Printable {
    //定义字符串的抽象方法
    void print(String s);
}

package com.MethodReference.ObjectMethodReference;

public class MethodRerObject {
    //定义一个成员方法,传递字符串,把字符串按照大写输出
    public void printUpperCaseString(String str){
        System.out.println(str.toUpperCase());
    }

}

package com.MethodReference.ObjectMethodReference;
/*
     通过对象名引用成员方法
     使用前提是对象名是已经存在的,成员方法也是已经存在的
     就可以使用对象名来引用成员方法
 */
public class Demo01ObjectMethodReference {
    //定义一个方法,方法的参数传递Printable接口
 public static void printString(Printable p){
     p.print("Hello");
 }

    public static void main(String[] args) {
     //调用printString方法,方法的参数Printable是一个函数式接口,所以可以传递Lambda表达式
        printString((s)->{
         //创建MethodRerObject对象
         MethodRerObject obj = new MethodRerObject();
         //调用MethodRerObject对象中的成员方法printUpperCaseString,把字符串按照大写输出
            obj.printUpperCaseString(s);
        });
        /*
           使用方法引用优化Lambda
           对象是已经存在的MethodRerObject
           成员方法也是已经存在的printUpperCaseString
           所以我们可以使用对象名引用成员方法
         */
        MethodRerObject obj = new MethodRerObject();
        printString(obj::printUpperCaseString);
    }
}

2.通过类名引用静态成员方法
  • 类已经存在,静态成员方法也已经存在

  • 就可以通过类名直接引用静态成员方法

package com.MethodReference.StaticMethodReference;
@FunctionalInterface
public interface Calcable {
    //定义一个抽象方法
    int calsAbs(int number);

}

package com.MethodReference.StaticMethodReference;
/*
     通过类名引用静态成员方法
     类已经存在,静态成员方法也已经存在
     就可以通过类名直接引用静态成员方法
 */
public class Demo01StaticMethodReference {
    //定义一个方法,方法的参数传递要计算绝对值的整数,和函数式接口Calcable
    public static int method(int number,Calcable c){
        return   c.calsAbs(number);
    }

    public static void main(String[] args) {
        //调用method方法,传递计算绝对值的整数,和Lambda表达式
        int num = method(-12, (n) -> {
            //对参数进行绝对值计算并返回
            return Math.abs(n);
        });
        System.out.println(num); //12
        /*
        使用方法引用优化Lambda
        Math类是存在的
        abs计算绝对值的静态方法也是存在的
        所以我们可以直接通过类名引用静态方法
         */
       int num2 =  method(-12,Math::abs);
        System.out.println(num2); //12
    }
}

3.通过super引用父类的成员方法
package com.MethodReference.SuperMethodReference;
     /*
       定义见面的函数式接口
      */
     @FunctionalInterface
public interface Greetable {
     //定义见面的方法
     void greet();
}

package com.MethodReference.SuperMethodReference;
/*
    定义父类
 */
public class Human {
    //定义一个sayHello的方法
    public void sayHello(){
        System.out.println("Hello,我是Human");
    }
}

package com.MethodReference.SuperMethodReference;
/*
   定义子类
 */
public class Man extends Human{
    //子类重写父类sayHello方法

    @Override
    public void sayHello() {
        System.out.println("Hello,我是Man");
    }

    //定义一个 方法参数传递Greetable接口
    public void method(Greetable g){
        g.greet();
    }
    public void show(){
        //调用method方法,方法的参数Greetable是一个函数式接口,可以传递Lambda
/*        method(()->{
            //创建父类Human对象
            Human h = new Human();
            //调用父类的sayHello方法
            h.sayHello();
        });*/

        //因为有子父类关系,所以存在的一个关键字super,代表父类,所以我们可以直接使用super调用父类的成员方法
//        method(()->{super.sayHello();});

        /*
           使用super引用父类的成员方法
           super是已经存在的
           父类的成员方法sayHello也是已经存在的
           所以我们可以直接使用super引用父类的成员方法
         */
        method(super::sayHello);

    }
    public static void main(String[] args) {    
        new Man().show();
    }
}

4.通过this引用本类的成员方法
package com.MethodReference.ThisMethodReference;
/*
    定义一个富有的函数式接口
 */
@FunctionalInterface
public interface Richable {
    //定义一个想买什么就买什么的方法
    void buy();
}

package com.MethodReference.ThisMethodReference;
/*
    通过this引用本类的成员方法
 */
public class Husband {
    //定义一个买房子的方法
    public void buyHouse(){
        System.out.println("北京二环内买一套四合院");
    }

    //定义一个结婚的方法,参数传递Richable接口
    public void marry(Richable r){
        r.buy();
    }

    //定义一个非常高兴的方法
    public void soHappy(){
        //调用结婚的方法,方法的参数Richable是一个函数式接口,传递Lambda表达式
        marry(()->{
            //使用this.成员方法,调用本来买房子的方法
            this.buyHouse();
        });
        /*
          使用方法引用优化Lambda表达式
          this是已经存在的
          本来的成员方法buyHouse也是已经存在的
          所以我们可以直接this直接引用本来的成员方法buyHouse
         */
        marry(this::buyHouse);
    }

    public static void main(String[] args) {
         new Husband().soHappy();
    }
}

5.类的构造器(构造方法)引用
package com.MethodReference.ConstructorMethodReference;

public class Person {
    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.MethodReference.ConstructorMethodReference;
  /*
     定义一个创建Person对象的函数式接口
   */
  @FunctionalInterface
public interface PersonBuilder {
      //定义一个方法,根据传递的姓名,创建传递对象返回
      Person builderPerson(String name);
}

package com.MethodReference.ConstructorMethodReference;

import javax.print.attribute.standard.PrinterName;

/*
     类的构造器(构造方法)引用
    */
public class Demo {
    //定义一个方法,参数传递姓名和PersonBuilder接口,方法中通过姓名创建Person对象
       public static void printName(String name,PersonBuilder pb){
           Person person = pb.builderPerson(name);
           System.out.println(person.getName());
       }

       public static void main(String[] args) {
            //调用printName方法,方法的参数PersonBuilder接口是一个函数式接口,可以传递Lambda
          printName("迪丽热巴",(String name)->{
              return new Person(name);
          });
          /*
          使用方法引用优化Lambda表达式
          构造方法new Person(String name)已知
          创建对象已知
          就可以使用Person引用new创建对象
           */
          printName("古力娜扎",Person::new);  //使用Person类的带参构造方法,通过传递的姓名创建对象
       }
}

6.数组的构造器引用
package com.MethodReference.ArrayMethodReference;
 /*
  定义一个创建数组的函数式接口
  */
 @FunctionalInterface
public interface ArrayBuilder {
     //定义一个创建int数组的方法,参数传递数组的长度,返回创建好的int类型数组
     int[] builderArray(int length);
}

package com.MethodReference.ArrayMethodReference;

import java.util.Arrays;

/*
     数组的构造器引用
   */
public class Demo {
    /*
       定义一个方法
       方法的参数传递创建数组的长度和ArrayBuilder接口
       方法内部根据传递的长度使用ArrayBuilder中的方法创建数组并返回
     */
    public static int[] creatArray(int length,ArrayBuilder ab){
       return ab.builderArray(length);
    }

      public static void main(String[] args) {
          //调用creatArray方法,传递数组长度和Lambda表达式
          int[] arr1 = creatArray(10, (len) -> {
              //根据数组的长度创建数组并返回
              return new int[len];
          });
          System.out.println(arr1.length); //10
          /*
            使用方法引用优化Lambda表达式
            创建的就是int[]数组
            数组的长度也是已知的
            就是可以使用方法引用
            int[]引用new,根据参数传递的长度来创建数组
           */
          int[] arr2 = creatArray(10, int[]::new);
          System.out.println(Arrays.toString(arr2));
          System.out.println(arr2.length);
      }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值