码迷,mamicode.com
首页 > 编程语言 > 详细

java8中的方法引用与构造器引用

时间:2019-05-06 10:26:57      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ble   def   this   tst   抽象   实例   数组   注意   employee   

java8中的方法引用与构造器引用

方法引用:若Lambda体中的内容有方法已经实现了,我们可以使用“方法引用”

主要的三种语法格式:

  1. 对象::实例名
  2. 类::静态方法名
  3. 类::实例方法名

注意:

  1. Lmabda体中调用方法的参数列表与返回值类型要与函数式接口中抽象方法的函数列表和返回值类型保持一致
  2. 若Lambda参数列表中的第一参数是 实例方法的调用者,而第二个参数是 实例方法的参数时,可以使用ClassName::method

    public class Employee {
    
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        super();
    }
    
    public  Employee(int age){
        this.age = age;
    }
    
    public Employee(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    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{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
    }
    //对象::实例方法名
    @Test
    public void test1(){
        PrintStream ps1 = System.out;
        Consumer<String> con = (x) -> ps1.println(x);
    
        PrintStream ps = System.out;
        Consumer<String> con1 = ps::println;
    
        Consumer<String> con2 = System.out::println;
        con2.accept("abcdef");
    }
    
    @Test
    public void test2(){
        Employee employee = new Employee();
        Supplier<String> sup = () -> employee.getName();
        String str = sup.get();
        System.out.println(str);
    
        Supplier<String> sup2 = employee::getName;
        String str2 = sup2.get();
        System.out.println(str2);
    
    }
    //类:静态方法名
    @Test
    public void test3(){
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
    
        Comparator<Integer> com1 = Integer::compare;
    }

    构造器引用

    格式:ClassName::new
    注意:需要调用的构造器的参数列表要与函数式接口中抽象方法的参数列表保持一致

    public class Employee {
    
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        super();
    }
    
    public  Employee(int age){
        this.age = age;
    }
    
    public Employee(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    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{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
    }
    //构造器引用
    @Test
    public void test5(){
        Supplier<Employee> sup = () -> new Employee();
    
        //构造器引用方式
        Supplier<Employee> sup2 = Employee::new;
        Employee employee = sup2.get();
        System.out.println(employee);
    }
    
    @Test
    public void test6(){
        Function<Integer, Employee> fun = (x) -> new Employee(x);
    
        Function<Integer, Employee> fun2 = Employee::new;
        Employee emp = fun2.apply(101);
        System.out.println(emp);
    
    }

    数组引用

    格式:Type[ ]::new;

    //数组引用
    @Test
    public void test7(){
        Function<Integer, String[]> fun = (x) -> new String[x];
        String[] strs = fun.apply(10);
        System.out.println(strs.length);
    
        Function<Integer, String[]> fun2 = String[]::new;
        String[] strs2 = fun2.apply(20);
        System.out.println(strs2.length);
    }

java8中的方法引用与构造器引用

标签:ble   def   this   tst   抽象   实例   数组   注意   employee   

原文地址:https://blog.51cto.com/pengge666/2389574

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!