【发布时间】:2018-07-27 15:06:48
【问题描述】:
我正在尝试在 Kotlin 中为父类和子类实现最简洁的组合。
这就像我的课程的样子。
sealed class Parent {
open val attribute : String = "initial value"
}
data class Child (
override val attribute: String
) : Parent ()
如果构造函数 Child() 为空,我希望将 attribute 值设置为 "initial value"。
如果我希望它是其他东西(不等于"initial value"),我希望有机会通过构造函数设置attribute 对象的attribute 值
在 Kotlin 中可以吗?
【问题讨论】:
-
你想专门用一个数据类来做这个吗?
标签: inheritance kotlin