Lambda表达式 —– 函数引用(推荐)

导读:本篇文章讲解 Lambda表达式 —– 函数引用(推荐),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

 1.函数引用

 1.1.静态方法的引用:

1.1语法:

        1.1.2.注意事项:

        1.1.3.示例:

1.2.非静态方法的引用:

        1.2.1.语法:

        1.2.1.注意事项:

 1.3.构造方法的引用:

        1.使用场景:

         2.语法:

         3.注意事项:

                示例:

1.4.对象方法的特殊引用:

 1.4.1实例:


 

接着上一节讲到,Lambda表达式其实是为了简化接口代码的实现,但是如果Lambda表达式中需要处理的业务逻辑过于复杂,我们会重新写一个方法。此时我们只需要在Lambda表达式中引用这个方法就即可。又或者需要处理的业务逻辑以及在其它方法中写好了,此时需要引用这个即可。

 1.函数引用

引用一个已经存在的方法,使其代替lambda表达式完成接口的实现。

 1.1.静态方法的引用:

  1. 1.1语法:

                类::静态方法

        1.1.2.注意事项:

                1.在引用方法的后面,不要添加小括号。

                2.引用的这个方法,参数(数量,类型)和返回值,必须和接口中定义的一致。

        1.1.3.示例:

                

public class Lambda {

    private static interface Calculate {

        int calculate(int a, int b);

    }

    public static void main(String[] args) {
        //函数引用
        Calculate calculate = Lambda::calculate;
        int calculate1 = calculate.calculate(10, 10);
        System.out.println(calculate1);

    }

    private static int calculate(int x, int y) {
        if (x > y) {
            return x - y;
        } else if (x < y) {
            return y - x;
        } else {
            return x + y;
        }
    }

}

1.2.非静态方法的引用:

        1.2.1.语法:

                对象::非静态方法

        1.2.1.注意事项:

                在引用的方法后面,不要添加小括号。

                引用的这个方法,参数(数量,类型)和返回值,必须要跟接口定义的一致。

        1.2.2.示例:

                

public class Lambda {

    private static interface Calculate2 {


        int calculate2(int a, int b);

    }

    public static void main(String[] args) {
        //-------------------非静态方法的引用-----------------------------
        Calculate2 calculate2 = new Lambda()::calculate2;
        int i = calculate2.calculate2(10, 10);
        System.out.println(i);
    }


    private int calculate2(int x, int y) {
        if (x > y) {
            return x - y;
        } else if (x < y) {
            return y - x;
        } else {
            return x + y;
        }
    }

}

 1.3.构造方法的引用:

        1.使用场景:

                如果一个函数式接口中定义的方法,仅仅是为了得到一个类的对象,那么可以使用构造方法的引用来简化这个方法的实现。

         2.语法:

                类名::new

         3.注意事项:

                可以通过接口中的方法参数,区分引用不同的构造方法。

                示例:

public class Lambda2 {

    private static class Person {
        String name;
        int age;

        public Person() {
            System.out.println("Person类的无参构造方法执行了!");
        }

        public Person(String name) {
            this.name = name;
            System.out.println("Person类的有参构造方法执行了");
        }

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
            System.out.println("Person类的两个参数的构造方法执行了");
        }

    }

    @FunctionalInterface
    private static interface GetPerson {
        Person get();
    }

    @FunctionalInterface
    private static interface GetPersonParameter {
        Person get(String name);
    }

    @FunctionalInterface
    private static interface GetPersonParameters {
        Person get(String name, int age);
    }

    public static void main(String[] args) {
        //1.使用lambda表达式,实现GetPerson接口
        GetPerson getPerson = Person::new;
        getPerson.get();
        GetPersonParameter getPersonParameter = Person::new;
        getPersonParameter.get("小明");
        GetPersonParameters getPersonParameters = Person::new;
        getPersonParameters.get("小明", 16);


    }
}

1.4.对象方法的特殊引用:

        介绍:如果在使用lambda表达式,实现某些接口的时候。lambda表达式中包含了某一个对象,此时方法体中,直接使用这个对象掉用它的某一个方法就可以完成整体的逻辑。其他的参数,可以作为调用方法的参数。

 1.4.1实例:

public class Lambda3 {

    private static class Person {
        private String name;

        private Integer age;

        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public void setNames(Integer a, Integer age) {
            this.age = age;
            this.name = name;

        }

    }


    private static interface MyInterface {

        void set(Person person, Integer a, Integer b);

    }

    public static void main(String[] args) {
        Person person = new Person();
        person.setName("小明");
//将接口的person类参数引用调用该类方法,将接口中的其它参数放入该方法中
        // MyInterface myInterface =(Person person1, Integer a, Integer age)->person1.setNames(a,age);
        MyInterface myInterface = Person::setNames;
        myInterface.set(person, 10, 10);
        System.out.println(person.getAge());
    }
}

        

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/105067.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!