Kotlin - 什么是lambda表达式

2022-04-20  本文已影响0人  杨0612
什么是lambda表达式
以下是使用lambda表达式的例子
class Test(val name: String) {
    private fun test1(t: ((p: String) -> Boolean)) {//1
        val isSuccess = t.invoke("yang")
    }
    
    private fun test2() {
        test1 {//2
            println(it)
            true
        }
    }
}
反编译看看
public final class Test {
    ......
    private final void test1(Function1 t) {//1
        boolean isSuccess = (Boolean)t.invoke("yang");
    }

    private final void test2() {//2
        this.test1(new Function1() {
            @Override
            public Object invoke(Object o) {
                System.out.println(o);
                return true;
            }
        });
    }
     ......
}


public interface Function1<in P1, out R> : Function<R> {3
    /** Invokes the function with the specified argument. */
    public operator fun invoke(p1: P1): R
}
总结

以上分析有不对的地方,请指出,互相学习,谢谢哦!

上一篇 下一篇

猜你喜欢

热点阅读