class Sample(private var s : String) {
init {
s += "B"
}
constructor(t: String, u: String) : this(t) {
this.s += u
}
}
初始化 Sample 类
Sample("T","U")
You will get a string response at variable <是的trong>是的是的trong> as "TBU".
Value "T" is assigned to s from the 主构造函数主构造函数 of 样本 class.
然后,init块立即开始执行; 它将向 <是的trong>是的是的trong>变量添加 "B"。
接下来是 辅助构造函数辅助构造函数转弯,现在 "U"被添加到 <是的trong>是的是的trong>变量变成 "TBU"。
主构造函数 不能包含 any code。初始化代码可以放置在 初始化程序块中,初始化程序块的前缀是 init关键字。
During an instance initialization, the 初始化程序块 are executed in the same order as they appear in the 阶级团体, interleaved with the property initializers: ...