在Kotlin中调用另一个默认构造函数

apo*_*che 9 kotlin anko

在Kotlin中,当一个类有多个构造函数时,我们如何在另一个构造函数中调用指定的(来自iOS世界,我找不到更好的名称)构造函数.

让我举个例子

final class LoadingButton: LinearLayout {
    var text:String? = null

    constructor(context: Context, text: String) : this(context) {
        this.text = text
    }
    constructor(context: Context) : super(context) {
        val t = this.text
        this.view {
            button(t) { /* ... */}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里,如果我这样做loadingButton("TEST", {}),那个字符串不会传播到按钮,因为this(context)方便构造函数中的代码之前调用(再次抱歉:).

可以在Kotlin解决吗?就像是

constructor(context: Context, text: String) {
    this.text = text
    this(context) 
}
Run Code Online (Sandbox Code Playgroud)

编辑

只是为了澄清这个想法,因为它被问到,想法是在一个活动中写这样的东西:

onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    verticalLayout {
        loadingButton("Some text")
        //or
        loadingButton() { this.text = "Some text"}
}
Run Code Online (Sandbox Code Playgroud)

这显然没那么有用,但你明白了.text属性可以在构造时或稍后知道.

这特别没用,因为Kotlin有参数的默认值,但我正在研究语言并遇到了这个问题.

编辑2

另一个澄清是我使用Anko进行布局,所以loadingButton方法如下所示:

inline fun ViewManager.loadingButton() = loadingButton {  }
inline fun ViewManager.loadingButton(init: LoadingButton.() -> Unit) = ankoView({ LoadingButton(it) }, init)
inline fun ViewManager.loadingButton(text: String, init: LoadingButton.() -> Unit) = ankoView({ LoadingButton(it, text) }, init)
Run Code Online (Sandbox Code Playgroud)

vod*_*dan 15

带有对构造函数的后调用的代码不能存在于JVM上,因为super(...) 对类本身执行任何操作之前必须调用它.可以把它想象成超类包含一个私有字段,你必须先使用它来初始化它.

这通常不是问题,因为您可以反过来调用构造函数:

constructor(context: Context, text: String?) : super(context) {
    this.text = text
    this.view {
        button(text) { /* ... */}
    }
}
constructor(context: Context) : this(context, null)
constructor(context: Context, text: String) : this(context, text)
Run Code Online (Sandbox Code Playgroud)

上面的代码与默认参数大致相同:

constructor(context: Context, text: String? = null) : super(context) {
    this.text = text
    this.view {
        button(text) { /* ... */}
    }
}
Run Code Online (Sandbox Code Playgroud)

要使此代码惯用(并简洁),请使用构造函数:

class LoadingButton(context: Context, val text: String? = null): LinearLayout(context) {
    init {
        this.view {
            button(text) { /* ... */}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

术语如下:指定 - 主要,便利 - 次要

有关更多详细信息,请参阅类和继承 - Kotlin编程语言.