constructor - 在构造函数中调用 super 并在 Kotlin 中分配一个字段?

标签 constructor kotlin

除了调用 super 之外,我想在构造函数中初始化 status 字段。

class MyException : RuntimeException {

    init {
        val status: Status
    }

    constructor(status: Status) : super()

    constructor(status: Status, cause: Throwable) : super(cause)

}

我怎样才能做到这一点?

最佳答案

这对我有用:

class MyException : RuntimeException {
    val status: Status

    constructor(status: Status) : super() {
        this.status = status
    }

    constructor(status: Status, cause: Throwable) : super(cause) {
        this.status = status
    }
}

关于constructor - 在构造函数中调用 super 并在 Kotlin 中分配一个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43040554/

相关文章:

c++ - 断然阻止或阻止带有特定参数的对象实例化

python - 简单的python继承

kotlin - Interface 的函数与 Property 的 getter 冲突

方法文档中的 Kotlin 静态字段值?

Java在另一个调用构造函数

c++ - 基本类型的 static_cast<T> 与 T(n)

Kotlin kovenant succesUi 在错误的线程上?

java - ARCore – 渲染距离相机 200 米的物体

java - Android kotlin accountkit - 无法正常工作

java - @Value 注解不从属性文件中注入(inject)值