jdk8新特性-方法引用

  1. 简单实例,解决Lambda表达式的冗余
  • 定义函数式接口
//函数式接口
@FunctionalInterface
public interface Printable {
    //定义一个可以打印的抽象方法
    void print(String s);
}
  • 调用函数式接口
public class Demo01PrintRef {
    public static void main(String[] args) {
        //Lambda表达式的形式
        printString(s-> System.out.println(s));

        /*
            方法引用的形式
            使用条件:
                1.System.out对象已经存在
                2.println方法已经存在
        */
        printString(System.out::println);
    }

    public static void printString(Printable p){
        p.print("Hello World");
    }
}
  1. 使用对象名引用成员变量
  • 函数式接口
//函数式接口
@FunctionalInterface
public interface Printable {
    //定义一个可以打印的抽象方法
    void print(String s);
}
  • 创建对象,包含被调用的成员方法
public class ObjectMethodRef {
    public void printUpperCaseString(String s){
        System.out.println(s.toUpperCase());
    }
}
  • 使用对象名引用成员变量的方式
/*
    通过对象名引用成员方法
    条件:
        1.对象名已经存在
        2.成员方法已经存在
 */
public class Demo02ObjectMethodRef {
    public static void main(String[] args) {
        //使用Lambda表达式的方式
        printUpperString((String s)->{
            ObjectMethodRef obj = new ObjectMethodRef();
            obj.printUpperCaseString(s);
        });
        //使用对象方法引用的方法
        //首先需要创建对象,使其存在
        ObjectMethodRef obj = new ObjectMethodRef();
        printUpperString(obj::printUpperCaseString);
    }
    public static void printUpperString(Printable p){
        p.print("Hello World");
    }
}
  1. 使用类名引用静态成员方法
  • 函数式接口
//函数式接口
@FunctionalInterface
public interface Calcable {
    //计算绝对值的抽象方法
    int calAbs(int number);
}
  • 类名引用静态成员方法
/*
    通过类名引用静态成员方法
    条件:
        1.类存在
        2.静态成员方法存在
 */
public class Demo03StaticMethodRef {
    public static void main(String[] args) {
        //使用Lambda表达式的形式
        int result1 = abs(-10, (int num) -> {
            return Math.abs(num);
        });
        System.out.println(result1);

        //使用类名引用静态成员方法
        int result2 = abs(-10, Math::abs);
        System.out.println(result2);
    }

    public static int abs(int number,Calcable cal){
        return cal.calAbs(number);
    }
}
  1. 使用super引用父类成员方法
  • 定义函数式接口
//定义函数式接口
@FunctionalInterface
public interface Greetable {
    //打招呼的抽象方法
    void greet();
}
  • 定义父类
//定义父类
public class Human {
    public void sayHello(){
        System.out.println("Hi,I am Human");
    }
}
  • 定义子类,并使用super引用父类成员方法
//定义子类
public class Man extends Human {
    @Override
    public void sayHello() {
        System.out.println("Hi,I am Man");
    }

    public void method(Greetable g){
        g.greet();
    }

    public void show(){
        //使用Lambda表达式
        method(()->{
            Human h = new Human();
            h.sayHello();
        });

        //使用Lambda表达式,直接super调用父类方法
        method(()->{
            super.sayHello();
        });

        //使用super引用父类成员
        method(super::sayHello);
    }

    public static void main(String[] args) {
        new Man().show();
    }
}
  1. 使用this引用本类成员方法
  • 定义函数式接口
//定义函数式接口
@FunctionalInterface
public interface Richable {
    //定义买房子的抽象方法
    void buyHouse();
}
  • 使用this引用本类成员方法
/*
    通过this引用本类的成员方法
 */
public class ThisMethodRef {
    public void buyHouse(){
        System.out.println("Get one big house in BeiJing");
    }
    public void marry(Richable richable){
        richable.buyHouse();
    }
    public void soHappy(){
        //使用Lambda表达式调用本类成员方法
        marry(()->{
            this.buyHouse();
        });
        //使用this引用本类成员方法
        marry(this::buyHouse);
    }

    public static void main(String[] args) {
        new ThisMethodRef().soHappy();
    }
}
  1. 使用类的构造器引用
  • 定义函数式接口
@FunctionalInterface
public interface PersonBuilder {
    //定义转换为Person类的抽象方法
    Person changeToPerson(String name);
}
  • 使用类的构造器引用
public class Demo6ConstructorMethodRef {
    public static void method(String name,PersonBuilder personBuilder){
        Person person = personBuilder.changeToPerson(name);
        System.out.println(person.getName());
    }

    public static void main(String[] args) {
        //Lambda表达式
        method("yorick",(String name)->{
            return new Person(name);
        });
        //使用类的构造器引用
        method("tom",Person::new);
    }
}
  1. 数组的构造器引用
  • 定义函数式接口
@FunctionalInterface
public interface ArrayBuilder {
    //定义根据长度创建int类型数组的抽象方法
    int[] buildArray(int length);
}
  • 数组的构造器引用
public class Demo07ArrayMethodRef {
    public static int[] createArray(int length,ArrayBuilder arrayBuilder){
        return arrayBuilder.buildArray(length);
    }

    public static void main(String[] args) {
        //Lambda表达式
        int[] array1 = createArray(8, (int length) -> {
            return new int[length];
        });
        System.out.println(array1.length);
        //使用数组的构造器引用
        int[] array2 = createArray(10, int[]::new);
        System.out.println(array2.length);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城老湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值