在kotlin类中创建二级构造函数的正确位置是什么?
在init块上方或下方创建二级构造函数的正确位置是什么在kotlin类中创建二级构造函数的正确位置是什么?,kotlin,Kotlin,在init块上方或下方创建二级构造函数的正确位置是什么 class Bottle(bottleShape: String, capacity: Int, color: String): Container( bottleShape, capacity) { private var closed: Boolean = true val bottleColor: String constructor(bottleShape: String, capacity: Int): this (
class Bottle(bottleShape: String, capacity: Int, color: String): Container( bottleShape, capacity) {
private var closed: Boolean = true
val bottleColor: String
constructor(bottleShape: String, capacity: Int): this (bottleShape, capacity, "Transparent")
init{
closeAble = true
bottleColor = color
println(bottleColor)
}
}
很明显,这只是一个需要秩序的惯例:
- 属性声明和初始值设定项块
- 二级构造函数
- 方法声明
- 伴星
我会把初始化放在二级构造函数之前。非常感谢u-soo