暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

方法引用

博博JAVA学习之路 2021-08-29
264

方法引用

方法引用 1

1.1. 定义: 方法引用就是Lambda表达式,函数式接口的一个实例。 2

1.2. 使用情景:当要传递给lambda体的操作,已经有实现方法了,则可以使用方法引用 2

1.3. 前提条件:参数列表(类型、数量),返回值类型一致即可,实现方法名称不用一致 2

1.4. 格式 2

1.5. 三种主要使用情况 3

1.5.1. 对象 :: 实例方法 3

1.5.2. :: 静态方法 3

1.5.3. :: 实例方法 3

1.6. 作用:就是可以将方法当做参数进行传递了 3

1.7. 条件:使用Lambda表达式实现函数式接口作为的参数,并且Lambda表达式中仅仅进行了其他方法的调用,即可使用方法引用,将Lambda表达式替换成方法引用 3

1.8. 使用 3

1.9. 构造引用:类::new 6

1.10. 数组引用:数组::new 10



1.1.定义:  

     方法引用就是Lambda表达式,函数式接口的一个实例。


    1.2.使用情景:

      当要传递给lambda体的操作,已经有实现方法了,则可以使用方法引用


      1.3.前提条件:

        参数列表(类型、数量),返回值类型一致即可,实现方法名称不用一致
          实现接口的抽象方法的参数列表和返回值类型,
          必须与方法引用的参数列表和返回值类型一样。

          1.4.格式

            使用操作符 :: 将类或对象与方法名分隔。
            三种主要使用情况:
             - 对象 :: 实例方法
             -   类 :: 静态方法
             -   类 :: 实例方法


            1.5.三种主要使用情况

            1.5.1.对象 :: 实例方法

              前提条件:参数列表数量类型,返回值类型必须一致。


              1.5.2.类 :: 静态方法

                前提条件:参数列表数量类型,返回值类型必须一致。

                1.5.3.类 :: 实例方法


                  本质:将第一个参数作为方法的调用者。
                    前提条件:
                    函数式接口中参数列表第一个参数,
                    可以当做对象调用实现方法,
                    并且其他参数当做实现方法的参数,
                    就可以使用类::实例方法调用。

                    1.6.作用:

                      就是可以将方法当做参数进行传递了


                      1.7.条件:

                        使用Lambda表达式实现函数式接口作为的参数,
                        并且Lambda表达式中仅仅进行了其他方法的调用,
                        即可使用方法引用,将Lambda表达式替换成方法引用


                        1.8.使用


                          package com.bobo.study.jdk.methodreference;


                          import java.util.Date;
                          import java.util.function.BiFunction;
                          import java.util.function.Supplier;
                          import java.util.function.ToIntBiFunction;


                          import org.junit.Test;


                          public class MethodReferenceTest {

                          @Test
                          public void testMethodReferenceTest1() {
                          System.out.println("==== 类::静态方法 示例 ---- begin ====");

                          System.out.println(testMethodAsParameter((x, y)->Integer.compare(x, y)));
                          System.out.println("----------------------------");
                          // 将方法当做参数进行传递了,其实本质还是Lambda表达式,只是省略了
                          System.out.println(testMethodAsParameter(Integer::compare));

                          System.out.println("==== 类::静态方法 示例 ---- end ====\n");

                          System.out.println("==== 对象::实例方法 示例 ---- begin ====");

                          Date date = new Date();

                          System.out.println(testObjectMethod(() -> date.getTime()));
                          System.out.println("----------------------------");
                          // 将方法当做参数进行传递了,其实本质还是Lambda表达式,只是省略了
                          System.out.println(testObjectMethod(date :: getTime));

                          System.out.println("==== 对象::实例方法 示例 ---- end ====\n");

                          System.out.println("==== 类::实例方法 示例 ---- begin ====");

                          System.out.println(testClassMethod((a,b) -> a.compareTo(b)));
                          System.out.println("----------------------------");
                          // compareTo是实例方法,在这里直接使用类调用,
                          // testClassMethod方法传递的参数是需要两个Integer参数的对象,
                          // 即ToIntBiFunction<Integer, Integer> tib
                          // compareTo方法实现是int compareTo(Integer anotherInteger)
                          // 即需要一个Integer对象调用compareTo方法,并且将另一个Integer对象当成参数
                          // 所以可以满足 [类::实例方法] 方法引用的条件
                          System.out.println(testClassMethod(Integer::compareTo));

                          System.out.println("==== 类::实例方法 示例 ---- end ====\n");
                          }
                          /**
                          * - 类::静态方法示例
                          * - 方法当做参数示例
                          * @param f
                          * @return
                          */
                          private Integer testMethodAsParameter(BiFunction<Integer, Integer, Integer> f) {
                          return f.apply(10, 20);
                          }

                          /**
                          * - 对象::实例方法示例
                          * - 方法当做参数示例
                          * @param s
                          * @return
                          */
                          private Long testObjectMethod(Supplier<Long> s) {
                          return s.get();
                          }

                          /**
                          * - 类::实例方法示例
                          * - 方法当做参数示例
                          * @param tib
                          * @return
                          */
                          private Integer testClassMethod(ToIntBiFunction<Integer, Integer> tib) {
                          return tib.applyAsInt(10, 20);
                          }

                          }


                          结果如下:

                            ==== 类::静态方法 示例 ---- begin ====
                            -1
                            ----------------------------
                            -1
                            ==== 类::静态方法 示例 ---- end ====


                            ==== 对象::实例方法 示例 ---- begin ====
                            1630246119597
                            ----------------------------
                            1630246119597
                            ==== 对象::实例方法 示例 ---- end ====


                            ==== 类::实例方法 示例 ---- begin ====
                            -1
                            ----------------------------
                            -1
                            ==== 类::实例方法 示例 ---- end ====




                            1.9.构造引用:类::new


                              构造引用是方法引用的特例
                              格式:类::new
                              等价:new 类()
                              特点:
                              参数列表最后一个是对应构造类的类型(包括父类及实现的接口),
                              剩下的参数都是构造函数的参数。

                              1.10.数组引用:数组::new


                                数组引用是方法引用的特例
                                格式:数组::new
                                等价:new 数组[]
                                特点:只有两个参数,最后一位是返回值类型及数组类型


                                构造引用与数组引用示例:


                                接口:

                                  package com.bobo.study.jdk.methodreference;


                                  public interface IPerson {


                                  }

                                  父类:

                                    package com.bobo.study.jdk.methodreference;


                                    public class PersonParent {

                                    public String sex;

                                    public PersonParent() {
                                    System.out.println("PersonParent => 无参构造");
                                    }

                                    public PersonParent(String sex) {
                                    this.sex = sex;
                                    System.out.println("PersonParent => 有参构造 sex = " + sex);
                                    }


                                    }

                                    子类:

                                      package com.bobo.study.jdk.methodreference;


                                      public class Person extends PersonParent implements IPerson{

                                      private int id;
                                      private String name;

                                      public Person() {
                                      System.out.println("无参构造");
                                      }

                                      public Person(int id) {
                                      this.id = id;
                                      System.out.println("仅有一个参数构造 => id = " + id);
                                      }

                                      public Person(int id, String name) {
                                      this.id = id;
                                      this.name = name;
                                      System.out.println("两个参数构造 => id = " + id + ", name = " + name);
                                      }


                                      public int getId() {
                                      return id;
                                      }


                                      public void setId(int id) {
                                      this.id = id;
                                      }


                                      public String getName() {
                                      return name;
                                      }


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


                                      @Override
                                      public String toString() {
                                      return "Person [id=" + id + ", name=" + name + ", sex=" + sex + "]";
                                      }

                                      }

                                      测试类:

                                        package com.bobo.study.jdk.methodreference;


                                        import java.util.Arrays;
                                        import java.util.function.BiFunction;
                                        import java.util.function.Function;
                                        import java.util.function.Supplier;


                                        import org.junit.Test;


                                        public class ConstructorReferenceTest {


                                        @Test
                                        public void testConstructorReference() {

                                        System.out.println("==== 构造引用 示例 ---- begin ====\n");

                                        Supplier<Person> s1 = () -> new Person();
                                        s1.get();
                                        // 返回类型为PersonParent父类
                                        Function<Integer, PersonParent> f1 = id -> new Person(id);
                                        f1.apply(10);
                                        // 返回类型为IPerson接口类
                                        BiFunction<Integer, String, IPerson> bf1 = (id, name) -> new Person(id, name);
                                        bf1.apply(10, "bobo");

                                        System.out.println("----------------------------");

                                        Supplier<Person> s2 = Person::new;
                                        s2.get();
                                        // 返回类型为PersonParent父类
                                        Function<Integer, PersonParent> f2 = Person::new;
                                        f2.apply(20);
                                        // 返回类型为IPerson接口类
                                        BiFunction<Integer, String, IPerson> bf2 = Person::new;
                                        System.out.println(bf2.apply(20, "bobo"));

                                        System.out.println("\n==== 构造引用 示例 ---- end ====\n");
                                        }

                                        @Test
                                        public void testArrayReference() {

                                        System.out.println("==== 数组引用 示例 ---- begin ====\n");

                                        Function<Integer, String[]> f1 = len -> new String[len];
                                        System.out.println(Arrays.toString(f1.apply(3)));

                                        System.out.println("----------------------------");


                                        Function<Integer, String[]> f2 = String[]::new;
                                        System.out.println(Arrays.toString(f2.apply(3)));

                                        System.out.println("\n==== 数组引用 示例 ---- end ====");
                                          }
                                        }



                                        结果如下:

                                          ==== 构造引用 示例 ---- begin ====


                                          PersonParent => 无参构造
                                          无参构造
                                          PersonParent => 无参构造
                                          仅有一个参数构造 => id = 10
                                          PersonParent => 无参构造
                                          两个参数构造 => id = 10, name = bobo
                                          ----------------------------
                                          PersonParent => 无参构造
                                          无参构造
                                          PersonParent => 无参构造
                                          仅有一个参数构造 => id = 20
                                          PersonParent => 无参构造
                                          两个参数构造 => id = 20, name = bobo
                                          Person [id=20, name=bobo, sex=null]


                                          ==== 构造引用 示例 ---- end ====


                                          ==== 数组引用 示例 ---- begin ====


                                          [null, null, null]
                                          ----------------------------
                                          [null, null, null]


                                          ==== 数组引用 示例 ---- end ====



                                          文章转载自博博JAVA学习之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                          评论