lambda方法的引用与构造方法引用

方法的引用

/**
 * @auther hhh
 * @date 2018/12/29 22:37
 * @description
 */
public class ObjectMethodUse {
    /**
     * 对象方法的引用(有限制条件)
     *      抽象方法的第一个参数类型刚好是实例方法的类型(函数式接口的抽象方法必须要有输入参数),抽象方法剩余
     *      的参数恰好可以当做实例方法的参数。如果函数式接口的实现能由上面说的实例方法调用来实现的话,
     *      那么就可以使用对象方法的引用(两个条件都要满足)
     * 语法:
     *      类名::instMethod
     */
    public void notUseObjectMethod(){
        //不能使用对象方法引用,因为其没有输入参数
        //比如如下函数式接口
        Runnable r = ()->{};
        Closeable c = () ->{};
        Supplier<String> s = ()->" ";
    }
    public static void main(String[] args) {
        //抽象方法的类型恰好是实例方法的类型
        //参数巧合是Too类型,调用的方法也是too的方法(Too too 与 new Too()是同种类型)
        //剩余的参数恰好可以当做实例方法的参数(Consumer<T>函数式接口没有返回值,Too中的foo也没有返回值)
        Consumer<Too> consumer = (Too too) -> new Too().foo();
        //可以使用对象方法引用
        Consumer<Too> consumer1 = Too::foo;

        consumer.accept(new Too());//invoke method
        consumer1.accept(new Too());//invoke method
        //不能转化为对象方法引用,不满足抽象方法的类型恰好是实例方法的类型
        Consumer<Too> consumer2 = (Too too) -> new Too1().foo();

        BiConsumer<Too1,String> biConsumer = (Too1 too,String s) -> new Too1().fun(s);
        BiConsumer<Too1,String> biConsumer1 = Too1::fun;
        biConsumer.accept(new Too1(),"输入参数,调用方法");
        biConsumer1.accept(new Too1(),"输入参数,调用方法");

        BiFunction<Prod,String,Integer> biFunction = (Prod s,String s1)->new Prod().fun(s1);
        //实例方法引用
        BiFunction<Prod,String,Integer> biFunction1 = Prod::fun;
        System.out.println(biFunction.apply(new Prod(),"12345"));//5
        System.out.println(biFunction1.apply(new Prod(),"12345"));//5

        //使用自定义函数式接口(有参数,且第一个参数类型为实例方法的类型)
        Execute2<Prod,String,String> execute2 = ( Prod,name,size) -> new Prod().fun2(name,size);
        Execute2<Prod,String,String> e= Prod::fun2;
    }
}
class Prod{
    Integer fun(String s){
        return s.length();
    }
    void fun2(String s,String s1){ }
}
//不符合使用对象方法引用,第一个参数需要是自定义的参数类型 T,不能是JDK自带的对象类型
@FunctionalInterface
interface Execute1{
    void run(String name,String size);
}
//修改成这样就可以了
@FunctionalInterface
interface Execute2<T,R,U>{
    void run(T t,R name, U size);
}
class Too{
    void foo(){
        System.out.println("invoke method");
    };
}
class Too1{
    void foo(){
        System.out.println("invoke method");
    };
    void fun(String s){
        System.out.println(s);
    }
}

构造方法的引用

/**
 * @auther hhh
 * @date 2018/12/30 11:19
 * @description 构造方法引用
 */
public class ConstructMethodUse {
    /**
     * 如果函数式接口的实现恰好可以通过调用一个类的构造方法来实现,那么就
     * 可以使用构造方法引用
     * 语法:
     *      类名::new
     */
    public static void main(String[] args) {
        //调用Person 无参构造函数
        Supplier<Person> personSupplier = ()->new Person();
        //由Supplier<Person> 可以推断出Person::new  中Person 的类型
        Supplier<Person> personSupplier1 = Person::new;
        personSupplier.get();//invoke person construct
        personSupplier1.get();//invoke person construct

        //需要有无参构造函数
        Supplier<List<String>> supplier = ArrayList::new;
        Supplier<Thread> threadSupplier = Thread::new;

        //有参构造函数
        Consumer<Integer> c1 = (i)->new Student(i);
        Consumer<Integer> c2 = Student::new;
        c1.accept(1);//有参构造
        c2.accept(1);//有参构造
        Function<String,Integer> function = s -> s.length();
        Function<String,Integer> function1 = String::length;
        //第一个参数String Student的参数类型,第一个参数是new Student实例的参数
        Function<String,Student> function2 = Student::new;//有参构造String
    }
}
class Person{
    public Person() {
        System.out.println("invoke person construct");
    }
}
class Student{
    public Student(int i) {
        System.out.println("有参构造");
    }
    public Student(String s) {
        System.out.println("有参构造String");
    }
    public Student() {
        System.out.println("无参构造");
    }
}

猜你喜欢

转载自www.cnblogs.com/huanghuanghui/p/10198938.html