jdk1.8新特性-方法引用与构造器引用

方法引用与构造器引用

方法引用

  • 方法引用可以看做是lambda表达式深层次的表达,换句话说,方法引用就是lambda表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法,可以认为是lambda表达式的一个语法糖
  • 要求:实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致
  • 格式: 使用操作符 “ :: ” 将类(或对象)与方法名分割开来
  • 如下三种主要使用情况:

    对象::实例方法名
    类::静态方法名
    类::实例方法名


/**
 * 方法引用使用
 * 1.使用情景:
 *   当要传递给lambda体的操作,已经有实现的方法了,可以使用方法引用
 *
 * 2.方法引用本质就是lambda表达式。而lambda表达式作为
 *  函数式接口的实例,所以方法引用,也是函数式接口的实例
 *
 * 3.使用格式  类(或者对象)::方法名
 *
 * 4.具体分为如下的三种情况
 *     对象:: 非静态方法
 *     类:: 静态方法
 *     类:: 费静态方法
 *
 * 5.方法引用使用的要求:要求接口中的抽象方法的形参列表
 *   和返回值类型与方法引用的方法的形参列表
 *   和返回值类型相同!(针对1或者针对2)
 */
public class MethodRefTest {

    /**
     * 情况1:对象::实例方法
     * Consumer中的void accept(T t)
     * PrintStream 中的void println(T t)
     */
    @Test
    public void test1(){
        Consumer<String> con1=s-> System.out.println(s);
        con1.accept("北京");

        System.out.println("************************");

        PrintStream ps=System.out;
        Consumer<String> con2= ps::println;
        con2.accept("南京");
    }


    /**
     * 情况1:对象::实例方法
     * Supplier中的T get()
     * Employee中的String getName()
     */
    @Test
    public void test2(){
     /* Supplier<String> supplier=new Supplier<String>() {
            @Override
            public String get() {
                return null;
            }
        };   */

        Supplier<String> supplier=()->"123";
        System.out.println(    supplier.get());

        Employee em=new  Employee(1001,"马化腾",34,6000.38);

        Supplier<String> supplier1=()->em.getName();
        System.out.println(    supplier1.get());

    }

    /**
     * 情况2:对象::静态方法
     * comparator中的int  compare(T t1,T t2)
     * Integer中的int compare(T t1,T t2)
     */
    @Test
    public void test3(){

        Comparator<Integer> comparator=new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        System.out.println(comparator.compare(15,20));
        System.out.println("*****************************");

        Comparator<Integer> comparator1=(o1,o2)->Integer.compare(o1,o2);
        System.out.println(comparator1.compare(15,20));
        System.out.println("*****************************");

        Comparator<Integer> comparator2=Integer::compareTo;
        System.out.println(comparator2.compare(20,15));

    }

    /**
     *
     * Function中的R  apply(T t)
     * Math中的Long round(Double d)
     */
    @Test
    public void test4(){

     /*   Function<Double,Long> function=new Function<Double, Long>() {
            @Override
            public Long apply(Double aDouble) {
                return Math.round(aDouble);
            }
        };*/

        Function<Double,Long> function=d-> Math.round(d);
        System.out.println(function.apply(13.5));
        System.out.println("************************");

        Function<Double,Long> function1=Math::round;
        System.out.println(function.apply(13.2));

    }
    /**
     * 情况三:类::非静态方法
     * comparator中的int  compare(T t1,T t2)
     * String中的int  t1.compareto(t2)
     */
    @Test
    public void test5(){

         Comparator<String> com=(s1,s2)->s1.compareTo(s2);
         System.out.println( com.compare("a","b"));
         System.out.println("************************");

         Comparator<String> com1=String::compareTo;
         System.out.println(com1.compare("b","a"));
    }


    /**
     * 情况三:类::非静态方法
     * BiPredicate中的boolean  test(T t1,T t2)
     * String中的boolean  t1.equals(t2)
     */
    @Test
    public void test6(){

        BiPredicate<String,String> biPredicate=String::equals;
        System.out.println(biPredicate.test("ab","ab"));

    }
    /**
     * 情况三:类::非静态方法
     * Function中的R  apply(T t)
     * Employee中的String  getName()
     */
    @Test
    public void test7(){
        Employee em=new  Employee(1001,"马化腾",34,6000.38);
        Function<Employee,String> function=Employee::getName;
        System.out.println(function.apply(em));

    }
}



构造器引用

一、构造器引用
和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致
抽象方法的返回值类型即为构造器所属的类型
二、数组引用
可以把数组看为特殊类,写法与构造器引用一致


/**
 *  一、构造器引用
 *      和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致
 *      抽象方法的返回值类型即为构造器所属的类型
 *
 *  二、数组引用
 *      可以把数组看为特殊类,写法与构造器引用一致
 *
 */
public class ConstructorRefTest {

    //构造器引用
    //Supplier 中的T get()
     @Test
     public void test1(){

   /*   Supplier<Employee> supplier=new Supplier<Employee>() {
             @Override
             public Employee get() {
                 return new   Employee(1001,"马化腾",34,6000.38);
             }
         };
         System.out.println(supplier.get());*/


         Supplier<Employee> supplier=()->new Employee(1001,"马化腾",34,6000.38);
         System.out.println(supplier.get());
         System.out.println("*******************************");


         Supplier<Employee> supplier1=Employee::new;
         System.out.println(supplier1.get());

     }

    /**
     * Function 中的R rpply(T t)
     */
    @Test
    public void test2(){

        Function<Integer,Employee> function=id->new Employee(id);

        System.out.println(function.apply(1));
        System.out.println("*******************************");


        Function<Integer,Employee> function1=Employee::new;
        System.out.println(function1.apply(100));
    }


    /**
     * BiFunction 中的R rpply(T t)
     */
    @Test
    public void test3(){
        BiFunction<Integer,String,Employee> biPredicate=Employee::new;

        System.out.println(biPredicate.apply(1,"aoteman"));

    }

    /**
     * 数组引用
     * Function 中的apply(T t)
     */
    @Test
    public void test4(){
      /* Function<Integer,String[]> function=new Function<Integer, String[]>() {
           @Override
           public String[] apply(Integer integer) {
               return new String[5];
           }
       };*/
        Function<Integer,String[]> function=( a)-> new String[a];
        System.out.println(function.apply(3).length);

        Function<Integer,String[]> function1=String[]::new;
        System.out.println(function1.apply(5).length);

    }

}

public class Employee {

    private int id;
    private String name;
    private int age;
    private double salary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return id == employee.id &&
                age == employee.age &&
                Double.compare(employee.salary, salary) == 0 &&
                Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, salary);
    }
    public Employee() {
        System.out.println("空参构造器");
    }
    public Employee(int id) {
        this.id=id;
        System.out.println("id参构造器" +id);
    }
    public Employee(int id,String name) {
        this.id=id;
        this.name=name;
        System.out.println("id参构造器" +id +"  name构造器:"+name);
    }


    public Employee(int id, String name, int age, double salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
}

本章内容引用尚硅谷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值