java学习笔记(12)——方法引用

public static void printString(PrintA p) {
        p.print("helloworld");
    }

    public static void main(String[] args) {
    //最繁杂写法
        printString(new PrintA() {
            @Override
            public void print(String s) {
                System.out.println(s);
            }
        });
    //Lambda表达式的简化版
        printString((s) -> System.out.println(s));
    //方法引用
        printString(System.out::println);

printString(System.out::println);
::
双冒号为引用运算符,所在表达式被称为方法引用
若是Lambda要表达的函数方案已经存在于某个方案语法的实现中,能够用此法代替Lambda
image.png数组

image.png

注意:app

Lambda中传递的参数必定是方法引用中的那个方法能够接收的类型,不然抛出异常

经过对象名引用成员方法

@FunctionalInterface
public interface PrintB {
    void print(String s);
}
public class MethodRerObject {
    public void printUpperCase(String str) {
        System.out.println(str.toUpperCase());
    }
}
public static void printString(PrintB p) {
        p.print("hellow");
    }
    public static void main(String[] args) {
        printString(new PrintB() {
            @Override
            public void print(String s) {
                MethodRerObject obj = new MethodRerObject();
                obj.printUpperCase(s);
            }
        });

        //Lambda表达式优化
        printString((s) -> {
            MethodRerObject obj = new MethodRerObject();
                obj.printUpperCase(s);
        });

        //方法引用优化:对象MethodRerObject已经存在
        //成员方法printUpperCase已经存在

        //建立MethodRerObject对象
        MethodRerObject obj = new MethodRerObject();
        printString(obj :: printUpperCase);
    }

经过类名引用静态成员方法

@FunctionalInterface
public interface Calculate {
    int calAbs(int a);
}
public static int absMethod(int a, Calculate cal) {
        return cal.calAbs(a);
    }

    public static void main(String[] args) {
        int number = absMethod(-10, new Calculate() {
            @Override
            public int calAbs(int n) {
                return Math.abs(n);
            }
        });

        //Lambda表达式优化
        int number2 = absMethod(-10, (n) -> {
            return Math.abs(n);
        });
        
        //在Lambda基础上优化
        //Math工具类存在,absMethod静态方法也存在
        int number3 = absMethod(-10, Math::abs);
    }

思考:ide

这几个主题里说的引用,都是在Lambda表达式中,用到的成员方法,静态成员方法。
第一个的obj.printUpperCase(s);
这个的return Math.abs(n);

经过super引用父类成员方法

函数式接口函数

@FunctionalInterface
public interface Greatable {
    void greet();
}

父类工具

public class Human {
    public void sayHello() {
        System.out.println("hello,hehe");
    }
}

子类优化

@Override
    public void sayHello() {
        System.out.println("hello,male");
    }

    public void method(Greatable g) {
        g.greet();
    }

    public void show() {
        //最繁杂方法
        method(() -> {
            Human h = new Human();
            h.sayHello();
        });
        //由于有子父类关系,存在一个关键字super
        //能够直接使用super调用父类方法
        method(() -> {
            super.sayHello();
        });

        //方法引用
        //Human h = new Human();  
        //由于存在继承关系,因此上面这个不须要了,super表明父类
        method(super::sayHello);
    }

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

经过this引用本类成员方法

@FunctionalInterface
public interface Richable {
    void buy();
}
public void buyHouse() {
        System.out.println("北京四合院");
    }

    public void getMarry(Richable r) {
        r.buy();
    }
    public void happy() {
        getMarry(new Richable() {
            @Override
            public void buy() {
                //为何不能用这个
                //由于这是匿名内部类写法,this只在本匿名内部类起做用
                this.; //XXXX!!!
            }
        });
        
        getMarry(() -> {
            this.buyHouse();
        });

        getMarry(this::buyHouse);
    }

    public static void main(String[] args) {
        new Human().happy();
    }

!!!!
此例中,能够看出Lambda表达式并非新建了一个类,因此this依旧能够代指本类,而使用匿名内部类,this仅仅在内部类起做用,并不能调用本类中的成员方法ui

Lambda表达式传递的参数,对应重写方法的参数this

类的构造器(构造方法)引用

格式:spa

类名称::new
@FunctionalInterface
public interface PersonInterface {
    Person13 builderPerson(String name);
}
public static void printName(String name, PersonInterface p) {
        Person13 person = p.builderPerson(name);

        System.out.println(person.getName());
    }

    public static void main(String[] args) {
        printName("hehe", (String name) -> {
            return new Person13(name);
        });

        //方法引用
        //new Person(String name)构造方法已知
        //建立对象 new已知
        //使用Person引用new建立对象
        printName("hahaha", Person13::new);
    }

new Person13(name)
这就是一个有参构造方法code

数组的构造器(构造方法)引用

@FunctionalInterface
public interface ArrayBuiler {

    int[] buildArray(int length);
}
public static int[] creatArray(int length, ArrayBuiler arr) {
        return arr.buildArray(length);
    }

    public static void main(String[] args) {
        int[] arr1 = creatArray(12, (len) -> {
            return new int[len];
        });

        //方法引用
        //已知建立int[]数组
        //数组长度也已知
        int[] ints = creatArray(12, int[]::new);

    }
相关文章
相关标签/搜索