Java8 Lambda表达式实战之方法引用(一)

方法的引用

方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法,方法引用提供了一种引用而不执行方法的方式,如果抽象方法的实现恰好可以使用调用另外一个方法来实现,就有可能可以使用方法引用

方法的引用分类

静态方法引用

 如果函数式接口的实现恰好可以通过调用一个静态方法来实现,那么就可以使用静态方法引用(静态方法的引用与实例方法的引用基本不受限制,只要满足调用条件即可)

静态方法引用示例代码:

/**
 * @auther hhh
 * @date 2018/12/28 23:02
 * @description 如果函数式接口的实现恰好可以通过调用一个静态方法来实现,那么就可以使用静态方法引用
 * 语法:类名::staticMethod
 * 注意:
 *      方法的引用不需要括号,因为其仅仅是方法的引用,并没有执行
 *      使用的函数式接口输入输出必须与定义的函数式接口一致
 */
public class StaticMethodUse{
    static <T> String hello(T s) {
        return s.toString();
    }
    static <T> String ret() {
        return "hello";
    }
    static void getSize(String s) {
        System.out.println(s.length());
    }
    static String toUpCase(String s){
        return s.toUpperCase();
    }
    public static void main(String[] args) {
        Supplier<String> supplier = () -> hello("hello");
        //使用方法引用 Supplier T get();
        // 1、只支持输出参数,不支持输入参数,所以一下方法报错
        //2、方法的引用为什么不需要括号?因为其仅仅是方法的引用,并没有执行
        //Supplier<String> stringSupplier = StaticMethodUse::hello("wqer");
        Supplier<String> s = StaticMethodUse::ret;
        Supplier<String> supplier1 = Fun::put;
        Function<Integer, String> function = StaticMethodUse::hello;
        //方法的参数必须在调用的时候传入,必须调用函数式接口
        System.out.println("function" + function.apply(123));

        //普通lambda使用
        Consumer<String> consumer = size -> StaticMethodUse.getSize(size);
        //方法的引用为什么不需要括号?因为其仅仅是方法的引用,并没有执行
        Consumer<String> c1 = StaticMethodUse::getSize;
        c1.accept("hello use lambda");

        //输出参数大写
        Function<String ,String> function1 = (ss) -> ss.toUpperCase();
        Function<String,String> function2 = StaticMethodUse::toUpCase;
        Function<String,String> function3 = Fun::toUpCase;
        System.out.println(function1.apply("aasdf"));
        System.out.println(function2.apply("aasdf"));
        System.out.println(function3.apply("aasdf"));
    }
}

class Fun {
    static String put() {
        return "hello";
    }
    static String toUpCase(String s){
        return s.toUpperCase();
    }
}

实例方法引用

如果函数式接口的实现恰好可以通过调用一个实例的实例方法来实现,那么就可以使用实例方法引用

示例代码:

/**
 * @auther hhh
 * @date 2018/12/28 23:29
 * @description 实例方法引用
 * 如果函数式接口的实现恰好可以通过调用一个实例的实例方法来实现,那么就可以使用实例方法引用
 * 语法: new instMethod()::method
 * 注意:后面不需要加括号也不需要加参数
 */
public class InstantMethodUse extends Base {
    String put() {
        return "hello";
    }

    public String toUpCase(String s) {
        return s.toUpperCase();
    }

    //调用当前实例的方法
    void test() {
        UnaryOperator<String> unaryOperator = this::toUpCase;
        System.out.println(unaryOperator.apply("this inst method"));
        //调用父类的实例方法
        UnaryOperator<String> u1 = super::toUpCase;
        System.out.println(u1.apply("use super upCase method"));
    }

    public static void main(String[] args) {
        Supplier<String> stringSupplier = () -> new InstantMethodUse().put();
        Supplier<String> stringSupplier1 = () -> {
            return new InstantMethodUse().put();
        };
        Supplier<String> stringSupplier2 = new InstantMethodUse()::put;
        //普通lambda表达式使用
        Supplier<String> stringSupplier3 = () -> new InstantMethodUse().put();
        System.out.println(stringSupplier2.get());
        //两种方式使用
        UnaryOperator<String> unaryOperator = new InstantMethodUse()::toUpCase;
        //新建对象引用
        InstantMethodUse instantMethodUse = new InstantMethodUse();
        UnaryOperator<String> unaryOperator1 = instantMethodUse::toUpCase;
        System.out.println(unaryOperator.apply("lambda instMethod Use") + "  ===  " + unaryOperator1.apply("lambda instMethod Use"));

        instantMethodUse.test();
    }
}
posted @ 2018-12-29 22:22  huanghaunghui  阅读(2648)  评论(0编辑  收藏  举报