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

时间:2019-02-03 05:10:02

标签: class inheritance kotlin compilation final

我正在拧my.kts脚本,并使用它来运行kotlin,我已经知道了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter your gmail here"
        android:textColor="@color/colorPrimary"
        android:textSize="30dp"
        android:layout_gravity="center"
        android:textAlignment="center"
        android:layout_marginTop="200dp"/>

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:inputType="textEmailAddress"
        android:hint="Enter your Gmail here"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="To Contunue"
        android:textAllCaps="false"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"/>

    </LinearLayout>

它无法编译,表示:

class TestA {
    init {}
    open fun testOpen() {
        println(this)
    }
}
class TestB : TestA {
    override fun testOpen() {
        super.testOpen()
    }
}

TestB类:TestA {

1 个答案:

答案 0 :(得分:1)

如果您从另一个类继承一个类,并且基类具有主构造函数,则必须对其进行初始化。您的<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="form-group row"> <label for="staticEmail" class="col-sm-2 col-form-label">Dein Ersparniss / Jahr</label> <div class="col-sm-10"> <input type="text" class="form-control" name="txtres" readonly> <input type="button" class="btn btn-danger" value="Berechnen" onClick="sumValues()"> </div> </div>具有默认的主要构造函数,因此应如下所示:

TestA

另一个问题是,默认情况下,kotlin中的类是final,因此您应该明确定义它们可以扩展:

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

查看this示例以获取更多信息。