【发布时间】:2017-07-09 23:52:10
【问题描述】:
假设我有这个 Java 和 Kotlin 接口:
public interface JavaInterface {
void onTest();
}
interface KotlinInterface {
fun onTest()
}
为什么我不能在没有构造函数的情况下创建 Kotlin 接口的实例?
// this is okay
val javaInterface: JavaInterface = JavaInterface {
}
// compile-time exception: interface does not have constructor
val kotlinInterface1: KotlinInterface = KotlinInterface {
}
// this is okay
val kotlinInterface2: KotlinInterface = object : KotlinInterface {
override fun onTest() {
}
}
为什么我不能像第一个示例一样创建KotlinInterface 的实例?
【问题讨论】:
-
这是这个问题的一个变种,我认为:stackoverflow.com/questions/44324821/…
-
@OliverCharlesworth 是的,先生,看起来很像。