Lambda表达式的方法引用

方法引用符 :: 该符号为引用运算符,而它所在的表达式被称为方法引用 

总结为四中类型:他们的格式分别为:

1、引用类的静态方法,格式       类名::静态方法 

2、引用对象的实例方法,格式     对象::成员方法 

3、引用类的实例方法,格式     类名::成员方法

4、引用构造器,格式    类名::new 


具体示例如下:

1、引用类的静态方法,格式       类名::静态方法 

比如:

Integer::parseInt ,Integer类的方法:public static int parseInt(String s) 将此String转换为int类型数据 ,方法一定为静态方法才可以。

代码示例如下:

Converter接口:

public interface Converter {
    int convert(String s);
}

ConverterTest运行测试类:

public class ConverterTest {
    public static void main(String[] args) {
        useConvert(s -> Integer.parseInt(s));
        //第二种:引用类方法,把String类型转换为int类型
        useConvert(Integer::parseInt);
    }
    public static void useConvert(Converter c){
        System.out.println(c.convert("520"));
    }
}

运行结果:

 

2、引用对象的实例方法

其实就是引用类中的成员方法
格式     对象::成员方法 

 

引用对象的实例方法 的 示例代码如下:

注意:Lambda表达式被对象的实例方法替代的时候,它的形式参数全部传递给该方法作为参数

Printer接口:

public interface Printer {
    void printUpperCase(String s);
}

PrintString类:

public class PrintString {
    public void printUpperCase(String s){
        System.out.println(s.toUpperCase());
    }
}

PrintTest测试类

public class PrinterTest {
    public static void main(String[] args) {
        usePrintUpperCase(s -> System.out.println(s.toUpperCase()));
        System.out.println("========");
        //第二种方法:引用对象的实例方法
        PrintString ps = new PrintString();
        usePrintUpperCase(ps::printUpperCase);
        //注意:Lambda表达式被对象的实例方法替代的时候,它的形式参数全部传递给该方法作为参数
    }

    private static void usePrintUpperCase(Printer p) {
        p.printUpperCase("I love you");
    }
}

输出结果:

3、引用类的实例方法,格式     类名::成员方法

示例代码如下:

MyString接口:

public interface MyString {
    String mySubString(String s, int a,int b);
}

MyStringTest运行测试类:

public class MyStringTest {
    public static void main(String[] args) {
        //Lambda简化写法
        useMyString((s,a,b)->{return s.substring(a, b);});
        //第二种方法:方法引用。引用类的实例方法
        useMyString(String::substring);
    }
    public static void useMyString(MyString my){
        String s = my.mySubString("abcdefgh",2, 6);
        System.out.println(s);
    }
}

 

运行结果:

 

4、引用构造器,格式    类名::new 

代码示例如下:

Employee类:

public class Employee {
    private String name;
    private int salary;

    public Employee() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getSalary() {
        return salary;
    }

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

Employee接口:

public interface EmployeeBuilder {
    Employee builder(String name, int salary);
}

运行测试类:

public class EmployeeTest {
    public static void main(String[] args) {
        //用Lambda的简化方法
        /*
        useEmployeeBuilder(new EmployeeBuilder() {
            @Override
            public Employee builder(String name, int salary) {
                Employee employee = new Employee(name, salary);
                return employee;
            }
        });
        */
        useEmployeeBuilder((s, a)->{return new Employee(s, a);});
        //第二种方法:引用构造器
        useEmployeeBuilder(Employee::new);
    }
    public static void useEmployeeBuilder(EmployeeBuilder e){
        Employee employee = e.builder("Kevin", 10000);
        System.out.println(employee.getName() + "薪资" + employee.getSalary());
    }
}

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值