Kotlin 中构造顺序
fun main(args: Array<String>) {
val foo = Foo("chen","")
}
class Foo(name: String) {
var name:String = name.capitalize()
init {
this.name = this.name +1
println(this.name)
}
constructor(name: String ,name1:String) : this(name){
this.name = this.name +2
println(this.name)
}
}
结果
Chen1
Chen12
构造函数的执行顺序
①先执行值域的直接初始化
②然后执行init代码块
③最后调用的构造函数
也就是说前面两个必定会执行