Java学习笔记(五十九)—— 方法引用

概述
  • 双冒号::为引用运算符,而他所在的表达式被称为方法引用。如果Lambda要表达的函数方案已经存在于某个方法的实现中,那么则可以通过双冒号来引用该方法作为Lambda的代替者

    /*定义打印的函数式接口*/
    @FunctionalInterface
    public interface Printable  {
        // 打印字符串的抽象方法
        void print(String s);
    }
    
        public static void printString(Printable p,String s){
            p.print(s);
            /*
                Lambda表达式的目的,打印参数传递的字符串
                把参数s传递给了System.out对象,调用对象中的方法println对字符串进行输出
                注意:
                    1、System.out对象已经存在
                    2、println方法已经存在
                使用方法引用来优化Lambda表达式
                可以使用System.out方法直接引用(调用)println方法
            */
    
        }
    
        public static void main(String[] args) {
            printString((s)->{
                System.out.println(s);
            },"hello");
            /*
                Lambda表达式的目的,打印参数传递的字符串
                把参数s传递给了System.out对象,调用对象中的方法println对字符串进行输出
                注意:
                    1、System.out对象已经存在
                    2、println方法已经存在
                使用方法引用来优化Lambda表达式
                可以使用System.out方法直接引用(调用)println方法
            */
            printString(System.out::println,"world");
        }
    
引用
  • 通过对象名引用成员方法

    /*定义打印的函数式接口*/
    @FunctionalInterface
    public interface Printable  {
        // 打印字符串的抽象方法
        void print(String s);
    }
    
    public class MethodRefObject {
        // 定义一个成员方法,传递字符串,把字符串按照大写输出
        public void printUpperCaseString(String s){
            System.out.println(s.toUpperCase());
        }
    }
    
        /*
            通过对象名引用成员(非静态)方法:
                1.对象名已经存在
                2.成员方法已经存在
        */
        public static void printString(Printable p,String s){
            p.print(s);
        }
        public static void main(String[] args) {
            printString((s)->{
                MethodRefObject obj=new MethodRefObject();
                obj.printUpperCaseString(s);
            },"hello"); // HELLO
    
    	 MethodRefObject obj=new MethodRefObject();
         printString(obj::printUpperCaseString,"hello"); // HELLO
        }
    
  • 通过类名引用成员方法

    @FunctionalInterface
    public interface Calcable {
        int calsAbs(int n);
    }
    
    /*
        通过类名引用静态方法
            1、类已经存在
            2、静态成员方法已经存在
    */
    public static int method(int n, Calcable c){
        return c.calsAbs(n);
    }
    public static void main(String[] args) {
        int number=method(-9,(n)->{
            return Math.abs(n);
        });
        System.out.println(number); // 9
        int number1=method(-10,Math::abs);
        System.out.println(number1); // 10
    }
    
  • 通过super引用成员方法,如果存在继承关系,当Lambda表达式出现super调用时,也可以使用方法引用进行替代

    // 定义函数式接口
    @FunctionalInterface
    public interface Greetable {
        void greet();
    }
    
    // 定义父类
    public class Human {
        public void sayHello(){
            System.out.println("hello,我是Human");
        }
    }
    
    // 定义一个子类
    public class Man extends Human{
        @Override
        public void sayHello() {
            System.out.println("hello 我是man");
        }
        public void method(Greetable g){
            g.greet();
        }
        public void show(){
            /*method(()->{
                //创建父类对象
                Human h = new Human();
                h.sayHello();
            });*/
            /*method(()->{
                //创建父类对象
                super.sayHello();
            });*/
            method(super::sayHello);
        }
    
        public static void main(String[] args) {
            new Man().show();
        }
    }
    
  • 通过this引用成员方法

    @FunctionalInterface
    public interface Richable {
        void buy();
    }
    
    public class Husband {
        public void buyHouse(){
            System.out.println("买房子");
        }
        public void marry(Richable r){
            r.buy();
        }
        public void soHappy(){
            /*marry(()->{
                this.buyHouse();
            });*/
            marry(this::buyHouse);
        }
    
        public static void main(String[] args) {
            new Husband().soHappy();
        }
    }
    
  • 类的构造器引用

    public class Personone {
        private String name;
    
        public Personone() {
        }
    
        public Personone(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    @FunctionalInterface
    public interface PersonBuilder {
         Personone BuilderPerson(String s);
    }
    
    public static void main(String[] args) {
        printName("迪丽热巴",(n)->{
           return new Personone(n);
        });
        printName("迪丽热巴",Personone::new);
    }
    public static void printName(String name,PersonBuilder pb){
        Personone p = pb.BuilderPerson(name);
        System.out.println(p.getName());
    
    }
    
  • 数组的构造器引用

    @FunctionalInterface
    public interface ArrayBuilder {
        int[] builderArray(int len);
    }
    
    public static int[] createArray(int len,ArrayBuilder ab){
        return ab.builderArray(len);
    }
    
    public static void main(String[] args) {
        int[] arrInt = createArray(3, (len) -> {
            return new int[len];
        });
        System.out.println(arrInt.length);
        int[] arrInt1 = createArray(3, int[]::new);
        System.out.println(arrInt1.length);
    }
    
发布了113 篇原创文章 · 获赞 1 · 访问量 948

猜你喜欢

转载自blog.csdn.net/weixin_44876003/article/details/103381388