Class 为什么kotlin脚本文件不能在同一文件中继承类?
我正在编写我的.kts脚本,并使用它运行kotlin,我得到了以下结果:Class 为什么kotlin脚本文件不能在同一文件中继承类?,class,inheritance,kotlin,compilation,final,Class,Inheritance,Kotlin,Compilation,Final,我正在编写我的.kts脚本,并使用它运行kotlin,我得到了以下结果: class TestA { init {} open fun testOpen() { println(this) } } class TestB : TestA { override fun testOpen() { super.testOpen() } } 它没有编译,说: error: this type is final, so it can
class TestA {
init {}
open fun testOpen() {
println(this)
}
}
class TestB : TestA {
override fun testOpen() {
super.testOpen()
}
}
它没有编译,说:
error: this type is final, so it cannot be inherited from
class TestB : TestA {
^
basic.kts:39:15: error: this type has a constructor, and thus must be initialized here
类TestB:TestA{如果您从另一个类继承了一个类,并且基类具有主构造函数,则必须对其进行初始化。您的
TestA
具有默认主构造函数,因此它应该如下所示:
class TestB : TestA() {
override fun testOpen() {
super.testOpen()
}
}
另一个问题是kotlin中的类默认为final,您应该明确定义它们可以扩展:
open class TestA
查看示例以了解更多信息。遵循以下示例: