Kotlin: Interface ...没有构造函数
我正在将我的一些Java代码转换为Kotlin,我不太了解如何实例化在Kotlin代码中定义的接口。例如,我有一个接口(在Java代码中定义):
public interface MyInterface {
void onLocationMeasured(Location location);
}
然后在我的 Kotlin 代码中,我进一步实例化了这个接口:
val myObj = new MyInterface { Log.d("...", "...") }
它工作正常。但是,当我将 MyInterface 转换为 Kotlin 时:
interface MyInterface {
fun onLocationMeasured(location: Location)
}
我收到一条错误消息:当我尝试实例化它时 - 尽管在我看来,除了语法之外没有任何变化。我是否误解了 Kotlin 中的接口工作方式?Interface MyListener does not have constructors