在Java中,我可以写这样的东西
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
handler.post(this);
}
});
当我在Kotlin中编写类似的内容时,出现编译错误。 lambda表达式内的
this
不能正常运行。我能做什么?val handler = Handler()
handler.post{
handler.post(this) // this line throws a compilation error
}
最佳答案
val handler = Handler()
fun doAction() {
handler.post(::doAction) // like `this`
}
handler.post(::doAction)
尽管从技术上讲Tim的解决方案也可行,但是他应该将其作为答案,因为这样比较容易。如果他这样做了,那就接受那个。
val handler = Handler() handler.post(object : Runnable { override fun run() { handler.post(this) } })
关于kotlin - 如何在Kotlin中从自身内部访问功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57692195/