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);
    }
}

你可能感兴趣的:(jdk8新特性-方法引用)