代码之家  ›  专栏  ›  技术社区  ›  Hind Forsum

为什么kotlin脚本文件不能在同一文件中继承类?

  •  0
  • Hind Forsum  · 技术社区  · 7 年前

    我正在编写我的.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 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{

    1 回复  |  直到 7 年前
        1
  •  1
  •   Feedforward    7 年前

    若从另一个类继承一个类,并且基类具有主构造函数,则必须对其进行初始化。你的 TestA

    class TestB : TestA() {
        override fun testOpen() {
            super.testOpen()
        }
    }
    

    open class TestA
    

    检查 this 有关更多信息,请参阅示例。

    推荐文章