0
点赞
收藏
分享

微信扫一扫

【Kotlin】Kotlin笔记8-Lambda编程3(Java的函数式API)


Kotlin笔记8-Lambda编程3-Java的函数式API


4.3 Lambda编程3

  • Java的函数式API

​Example-Java:​

interface Runnable {
void run();
}
new Thread(Runnable() {
@Override
public void run() {
System.out.println("Thread is running");
}
}).start();

​Kotlin:​

Thread(object : Runnable {
override fun run() {
println("Thread is running!")
}
}).start()

简化过程

​单独方法run,没有显示重写:​

Thread(Runnable {
println("Thread is running!")
}).start()

​参数列表唯一,接口名省略:​

Thread({
println("Thread is running!")
}).start()

​表达式移到括号外,括号省略:​

Thread{ println("Thread is running!") }.start()

实用场景
​​​OnClickListener-Java:​

public interface OnClickListener {
void onClick(View v);
}

​kotlin:​

button.setOnClickListener {
}


举报

相关推荐

0 条评论