something about SAM
Posted scaventz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了something about SAM相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章