构造器引用与数组引用

import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 一、构造器引用
 * 和方法引用类似,函数式接口的抽象方法的形参列表和构造器的一致
 * 抽象方法的返回值类型即为构造器所属的类的类型
 * 二、数组引用
 */
public class ConstructorRefTest {
    /**
     * 构造器引用
     * Supplier中的T get()
     */
    @Test
    public void test1() {
        Supplier<Employee> supplier = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println("************************");
        Supplier<Employee> supplier1 = () -> new Employee();
        System.out.println(supplier1.get());
        System.out.println("************************");
        Supplier<Employee> supplier2 = Employee::new;
        System.out.println("supplier2 = " + supplier2.get());
    }

    /**
     * Function中的R apply(T t)
     */
    @Test
    public void test2() {
        Function<Integer, Employee> function1 = id -> new Employee(id);
        Employee employee = function1.apply(1001);
        System.out.println("employee = " + employee);
        Function<Integer, Employee> function2 = Employee::new;
        System.out.println("function2 = " + function2.apply(1002));


    }

    /**
     * BiFunction中的R apply(T t,U u)
     */
    @Test
    public void test3() {
        BiFunction<Integer, String, Employee> function1 = (id, name) -> new Employee(id, name);
        Employee employee = function1.apply(1001, "Tom");
        System.out.println("employee = " + employee);
        BiFunction<Integer, String, Employee> function2 = Employee::new;
        System.out.println("function2 = " + function2.apply(1002, "Tom"));
    }

    /**
     * 数组引用
     * Function中的R apply(T t)
     */
    @Test
    public void test4() {
        Function<Integer, String[]> function1 = length -> new String[length];
        String[] strings = function1.apply(5);
        System.out.println("strings = " + Arrays.toString(strings));
        System.out.println("****************************");
        Function<Integer, String[]> function2 = String[]::new;
        String[] string2 = function2.apply(10);
        System.out.println("strings2 = " + Arrays.toString(string2));
    }
}

Employee.java

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * Created by cute coder
 * 2021/9/10 20:56
 */
@Getter
@Setter
@NoArgsConstructor
@Builder
@ToString
@EqualsAndHashCode
public class Employee {
    private int id;
    private String name;
    private int age;
    private double salary;

    public Employee(int id) {
        this.id = id;
        System.out.println("Employee(int id) ");
    }

    public Employee(int id, String name) {
        this.id = id;
        this.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、付费专栏及课程。

余额充值