方法引用

1 Lambda冗余问题以及方法引用初识


/**
 * 函数式接口
 *
 * @author Anonymous
 */
@FunctionalInterface
interface PrintMethod {
    void print(String str);
}

/**
 * Lambda冗余问题
 *
 * @author Anonymous 2020/3/12 15:53
 */
public class Demo1 {
    public static void main(String[] args) {
        /*
         使用Lambda表达式来展示字符串
         IDEA有一个提示,这里可以继续优化

         目标:
            testPrint方法,需要将String str交给 PrintMethod进行处理
            PrintMethod处理的方式是System.out.println

            PrintMethod是一个函数式接口,可以使用Lambda表达式完成代码
            但是貌似和这个接口,这里Lambda不够接近目标

            明确:
                1. 我要用System.out对象
                2. 需要使用println方法
                3. 需要处理的数据是明确的
         */
        testPrint("郑州加油!!!", str -> System.out.println(str));

        // 利用方法引用来处理当前代码
        /*
        调用对象:
            System.out
        执行方法:
            println方法
        需要处理的数据(可推导,可省略)
            "中国加油!!!祖国万岁!!!"
        ::
            Java中【方法引用】使用的运算符,标记
         */
        testPrint("中国加油!!!祖国万岁!!!", System.out::println);
    }

    /**
     * 该方法是一个使用函数式接口作为方法参数的一个方法
     *
     * @param str String类型的字符串
     * @param pm  PrintMethod函数式接口
     */
    public static void testPrint(String str, PrintMethod pm) {
        pm.print(str);
    }
}

 

2 方法引用小要求

 

 testPrint("郑州加油!!!", str -> System.out.println(str));
 
 testPrint("郑州加油!!!", System.out::println);
 
 1. 明确对象
     对象 ==> 调用者
     类对象,类名,super,this,构造方法,数组构造方法
 2. 明确的执行方法
     该方法只有名字不需要显式出现参数
 3. 需要处理的数据
     【联想,推导,省略】
 4. :: 方法引用格式

 

3 通过类对象来执行方法引用

 

1. 明确对象
    类对象
2. 明确执行的方法
    自定义
3. 处理的数据
    简单要求为String类型

 


/**
 * 函数式接口
 *      明确约束方法
 *          方法类型是
 *              参数是String类型参数
 *              没有返回值
 */
@FunctionalInterface
public interface Printable {
    void method(String str);
}
```

`````java
package com.qfedu.e_objectmethodreference;

/**
 * 自定义类
 */
public class ObjectMethodReference {
    /**
     * ObjectMethodReference类内的【成员方法】
     *     1. 参数是String类型
     *     2. 没有返回值
     * @param str 参数
     */
    public void print(String str) {

        System.out.println(str.toLowerCase());
    }

    /**
     * ObjectMethodReference类内的【成员方法】
     *     1. 参数是String类型
     *     2. 没有返回值
     * @param str 参数
     */
    public void printSubstring(String str) {
        System.out.println(str.substring(3));
    }

    public void saolei() {
        System.out.println("无参数方法");
    }
}


/**
 * 类对象使用方法引用展示
 *
 * @author Anonymous 2020/3/12 16:18
 */
public class Demo2 {
    public static void main(String[] args) {
        /*
        test方法需要使用一个Printable接口,执行对应的print方法
        这里需要引用ObjectMethodReference类对象对应的print方法,该方法有展示能力
         */
        ObjectMethodReference obj = new ObjectMethodReference();

        /*
        执行的对象:
            ObjectMethodReference的类对象
        明确执行的方法:
            print方法
        执行的目标:
            "ABCDE" 可省略,可推导
         */
        test(obj::printSubstring);
    }

    /**
     *
     * @param printable 函数式接口,约束的是方法类型
     */
    public static void test(Printable printable) {
        printable.method("ABCDE");
    }
}

 

4 通过类名来执行方法引用


import java.util.List;

/**
 * 函数式接口
 */
@FunctionalInterface
public interface PrintList {
    /**
     * 方法参数为List集合
     * 没有返回值
     * @param list List集合
     */
    void print(List<?> list);
}
```

```java
package com.qfedu.f_classmethodreference;

import java.util.List;

/**
 * 通过列明调用的静态方法,演示类
 */
public class ClassMethodReference {
    /**
     * 该方法符合要求
     * 没有返回值
     * @param list list集合
     */
    public static void showListInfo(List<?> list) {
        for (Object o : list) {
            System.out.println(o);
        }
    }
}

 


import java.util.ArrayList;

/**
 * 通过类名来执行方法引用
 *
 * @author Anonymous 2020/3/12 16:39
 */
public class Demo3 {
    public static void main(String[] args) {
        /*
         明确调用对象
            当前方法是一个静态成员方法,需要通过类名调用
         明确调用方法
            showListInfo
         明确的数据
            ArrayList<String> list = new ArrayList<>();
            可省略,可推导
         */
        testClass(ClassMethodReference::showListInfo);


        /*
        Lambda表达式
         */
        testClass(list -> {
            for (Object o : list) {
                System.out.println(o);
            }
        });
    }

    /**
     * 利用了一个函数式接口作为方法的参数
     *
     * @param printList 函数式接口
     */
    public static void testClass(PrintList printList) {
        ArrayList<String> list = new ArrayList<>();

        list.add("BMW");
        list.add("Audi");

        printList.print(list);
	}

}

 

 

5 通过super关键字执行方法引用

 


/**
 * @author Anonymous 2020/3/12 16:52
 */
public interface SaySomeThing {
    void say();
}
```

```java
package com.qfedu.g_supermethodreference;

/**
 * @author Anonymous 2020/3/12 16:53
 */
public class Father {
    public void sayHello() {
        System.out.println("你好 Java");
    }
}
```

```java
package com.qfedu.g_supermethodreference;

import java.util.Scanner;

/**
 * @author Anonymous 2020/3/12 16:53
 */
public class Son extends Father {

    public static void main(String[] args) {
        //lambda
        testSay(() -> System.out.println("你好"));

        new Son().sonMethod();
    }

    /**
     * 这里的参数是一个函数式接口,这里需要提供给一个符合要求的方法
     * @param sst 函数式接口参数
     */
    public static void testSay(SaySomeThing sst) {
        sst.say();
    }

    public void sonMethod() {
        /*
        父类中有一个无参数无返回值的sayHello
        满足当前SaySomeThing函数式接口,方法要求

        执行的对象
            super关键字,因为该方法在父类内
        执行的方法:
            sayHello
        无需参数
         */
        testSay(super::sayHello);
    }
}

 


/**
 * @author Anonymous 2020/3/12 17:00
 */
public interface ORM {
    /**
     * int ==> String
     * @param i int类型
     * @return String类型
     */
    String toStringType(int i);
}
```

```java
package com.qfedu.h_thismethodreference;

/**
 * @author Anonymous 2020/3/12 17:01
 */
public class ThisDemo {
    public void test() {
        String s = testThis(i -> i + ":");
        System.out.println(s);

        /*
        调用方法的对象
            this
        执行的方法:
            turn方法
        处理的数据
            10 (可省略,可联想)
         */
        String s1 = testThis(this::turn);
    }

    public String turn(int i) {
        return i + ":字符串";
    }

    public static String testThis(ORM orm) {
        return orm.toStringType(10);
    }

    public static void main(String[] args) {
        new ThisDemo().test();
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值