首页 > 编程语言 > 详细

Java基础教程——方法引用

时间:2019-07-14 09:10:10      阅读:93      评论:0      收藏:0      [点我收藏+]

方法引用

Lambda表达式的代码,是否可以再简洁?——方法引用

对象/类名::方法名
参数都不用写明。

import java.util.function.Consumer;
public class 方法引用 {
    static void printStr(Consumer<String> c, String s) {
        // 借助Consumer接口,accept方法可以接收参数
        c.accept(s);
    }
    public static void main(String[] args) {
        System.out.println("------ 方法引用(关注做什么,不管怎么做)");
        printStr(s -> System.out.println(s), "Lambda写法");
        // 使用方法引用的前提:
        // |--对象已经存在(System.out)
        // |--方法也存在(println())
        // |--参数是方法可接收类型(String)
        printStr(System.out::println, "方法引用:连参数都不写");
    }
}
------ 方法引用(关注做什么,不管怎么做)
Lambda写法
方法引用:连参数都不写

引用普通方法和静态方法

import java.util.function.Consumer;
public class 方法引用2 {
    public static void main(String[] args) {
        // -----------------------
        String str = "Journey to the west";
        printStr(s -> {
            MyString ms = new MyString();
            ms.printUpperCase(s);
        }, str);
        MyString ms = new MyString();
        printStr(ms::printUpperCase, str);
        System.out.println("--- 引用静态方法,直接类名::方法名");
        printStr(MyString::staticPrint, "Journey to the west");
    }
    static void printStr(Consumer<String> p, String s) {
        // 借助Consumer接口,accept方法可以接收参数
        p.accept(s);
    }
}
class MyString {
    public void printUpperCase(String s) {
        System.out.println(s.toUpperCase());
    }
    public static void staticPrint(String s) {
        System.out.println(s.toLowerCase());
    }
}
JOURNEY TO THE WEST
JOURNEY TO THE WEST
--- 引用静态方法,直接类名::方法名
journey to the west

引用父类方法(super)和自身方法(this)

import java.util.function.Consumer;
public class 方法引用3 {
    public static void main(String[] args) {
        new S().show();
    }
}
class F {
    void m(String s) {
        System.out.println("父类方法:" + s);
    }
}
class S extends F {
    @Override
    void m(String s) {
        System.out.println("子类方法:" + s);
    }
    public void invoke(Consumer<String> c, String s) {
        c.accept(s);
    }
    public void show() {
        // Lambda表达式写法
        invoke((s) -> {
            F f = new F();
            f.m(s);
            this.m(s);
        }, "Lambda表达式写法");
        invoke(super::m, "引用方法,使用super::");
        invoke(this::m, "引用方法,使用this::");
    }
}
父类方法:Lambda表达式写法
子类方法:Lambda表达式写法
父类方法:引用方法,使用super::
子类方法:引用方法,使用this::

引用构造方法(实例化对象)

import java.util.Arrays;
import java.util.function.Function;
class Cat {
    private String name;
    public Cat(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public class 方法引用4new {
    public static void main(String[] args) {
        createCat("Lambda(1)", (name) -> {
            return new Cat(name);
        });
        createCat("Lambda(2)简化,省略{}、return、;", (name) -> new Cat(name));
        createCat("方法引用:类::new", Cat::new);
        // ---创建数组
        createArray(10, (len) -> new Cat[len]);
        createArray(10, Cat[]::new);
    }
    static void createCat(String name, Function<String, Cat> f) {
        Cat c = f.apply(name);
        System.out.println(c.getName());
    }
    static void createArray(Integer len, Function<Integer, Cat[]> f) {
        Cat[] c = f.apply(len);
        System.out.println(Arrays.toString(c));
    }
}
Lambda(1)
Lambda(2)简化,省略{}、return、;
方法引用:类::new
[null, null, null, null, null, null, null, null, null, null]
[null, null, null, null, null, null, null, null, null, null]

Java基础教程——方法引用

原文:https://www.cnblogs.com/tigerlion/p/11182864.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!