码迷,mamicode.com
首页 > 其他好文 > 详细

something about SAM

时间:2021-06-02 15:03:35      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:val   rri   lan   ring   runnable   invoke   lang   lambda   over   

kotlin code:

fun go(r: Runnable) = r.run()

//fun fGo(f: () -> Unit) = go(f)

fun main() {
    val g: () -> Unit = { println("what")}
    go(g)
}

equivalent java code:

package temp;

import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.Lambda;

/**
 * @author Xin Wang
 * @date 5/28/2021
 */

public class Temp2Kt {
    public final static void go(Runnable r) {
        r.run();
    }

    public static void main(String[] args) {


        final class Temp2Kt$sam$java_lang_Runnable$0 implements Runnable {
            private final Function0 function;

            public Temp2Kt$sam$java_lang_Runnable$0(Function0 function) {
                super();
                this.function = function;
            }

            @Override
            public void run() {
                final Object result = function.invoke();
            }
        }

        //////////////main
        final Temp2Kt$main$g$1 instance = Temp2Kt$main$g$1.INSTANCE;
        final Temp2Kt$sam$java_lang_Runnable$0 runnable = new Temp2Kt$sam$java_lang_Runnable$0(instance);
        go(runnable);
    }
}

// this is an inner class of Temp2Kt
final class Temp2Kt$main$g$1 extends Lambda implements Function0 {

    public final static Temp2Kt$main$g$1 INSTANCE = new Temp2Kt$main$g$1();

    public Temp2Kt$main$g$1() {
        super(0);
    }

    @Override
    public Object invoke() {
        this.invoke1();
        return null;
    }

    public final void invoke1() {
        System.out.println("what");
    }
}



something about SAM

标签:val   rri   lan   ring   runnable   invoke   lang   lambda   over   

原文地址:https://www.cnblogs.com/scaventz/p/14823939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!