Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在kotlin类中创建二级构造函数的正确位置是什么?_Kotlin - Fatal编程技术网

在kotlin类中创建二级构造函数的正确位置是什么?

在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 (

在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 (bottleShape, capacity, "Transparent")

    init{

        closeAble = true
        bottleColor = color
        println(bottleColor)

    }
}

很明显,这只是一个需要秩序的惯例:

  • 属性声明和初始值设定项块
  • 二级构造函数
  • 方法声明
  • 伴星

我会把初始化放在二级构造函数之前。

非常感谢u-soo