相关疑难解决方法(0)

在Kotlin中调用超类构造函数,Super不是表达式

我有两个班Entity,并Account

abstract class Entity(
    var id: String? = null,
    var created: Date? = Date()) {

    constructor(entity: Entity?) : this() {
        fromEntity(entity)
    }

    fun fromEntity(entity: Entity?): Entity {
        id = entity?.id
        created = entity?.created
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

data class Account( 
    var name: String? = null,
    var accountFlags: Int? = null
) : Entity() {

    constructor(entity: Entity) : this() {
        super(entity)
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我错误

超级不是表达式,它只能用在点'左'的左侧.

为什么我不能这样做?

以下将传递编译错误,但我不确定它是否正确.

 constructor(entity: Entity) : this() {
    super.fromEntity(entity)
}
Run Code Online (Sandbox Code Playgroud)

kotlin

29
推荐指数
2
解决办法
3万
查看次数

标签 统计

kotlin ×1