kotlin 中的 init 块和构造函数有什么区别?
What is the difference between init block and constructor in kotlin?
我开始学习 Kotlin。我想知道 init
块和 constructor
之间的区别。
这与我们如何使用它来改进有什么区别?
class Person constructor(var name: String, var age: Int) {
var profession: String = "test"
init {
println("Test")
}
}
Kotlin 中的 class class 一个不包含代码的主构造函数(class 名称之后的构造函数),它只能初始化属性(例如 class X(var prop: String)
).
init{..}
块,您可以在其中放置更多代码,运行在 属性初始化后:
initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers
有关更多信息,请参阅 https://kotlinlang.org/docs/reference/classes.html#constructors
这是一个例子:
class X(var b: String) {
val a = print("a")
init {
print("b")
}
constructor() : this("aaa") {
print("c")
}
}
X()
调用时打印 abc
。因此 init{..}
在 之后 主要构造函数但在次要构造函数之前被调用。
init 块将在主构造函数之后立即执行。初始化程序块有效地成为主构造函数的一部分。 构造函数是辅助构造函数。对主构造函数的委托作为辅助构造函数的第一条语句发生,因此所有初始化块中的代码都在辅助构造函数主体之前执行。
例子
class Sample(private var s : String) {
constructor(t: String, u: String) : this(t) {
this.s += u
}
init {
s += "B"
}
}
认为您用
初始化了样本 class
Sample("T","U")
您将在变量 s 处得到一个字符串响应作为 "TBU"
。
值"T"
从Sampleclass的主构造函数赋值给s,然后立即开始执行init块它会将 "B"
添加到变量中。 init block secondary constructor block 开始执行后 s 会变成 "TBU"
.
自从,
The primary constructor cannot contain any code.
https://kotlinlang.org/docs/reference/classes.html
init 块允许向主构造函数添加代码。
如 Kotlin 文档中所述:
The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init
keyword.
During an instance initialization, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers: ...
我开始学习 Kotlin。我想知道 init
块和 constructor
之间的区别。
这与我们如何使用它来改进有什么区别?
class Person constructor(var name: String, var age: Int) {
var profession: String = "test"
init {
println("Test")
}
}
Kotlin 中的 class class 一个不包含代码的主构造函数(class 名称之后的构造函数),它只能初始化属性(例如 class X(var prop: String)
).
init{..}
块,您可以在其中放置更多代码,运行在 属性初始化后:
initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers
有关更多信息,请参阅 https://kotlinlang.org/docs/reference/classes.html#constructors
这是一个例子:
class X(var b: String) {
val a = print("a")
init {
print("b")
}
constructor() : this("aaa") {
print("c")
}
}
X()
调用时打印 abc
。因此 init{..}
在 之后 主要构造函数但在次要构造函数之前被调用。
init 块将在主构造函数之后立即执行。初始化程序块有效地成为主构造函数的一部分。 构造函数是辅助构造函数。对主构造函数的委托作为辅助构造函数的第一条语句发生,因此所有初始化块中的代码都在辅助构造函数主体之前执行。
例子
class Sample(private var s : String) {
constructor(t: String, u: String) : this(t) {
this.s += u
}
init {
s += "B"
}
}
认为您用
初始化了样本 classSample("T","U")
您将在变量 s 处得到一个字符串响应作为 "TBU"
。
值"T"
从Sampleclass的主构造函数赋值给s,然后立即开始执行init块它会将 "B"
添加到变量中。 init block secondary constructor block 开始执行后 s 会变成 "TBU"
.
自从,
The primary constructor cannot contain any code.
https://kotlinlang.org/docs/reference/classes.html
init 块允许向主构造函数添加代码。
如 Kotlin 文档中所述:
The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the
init
keyword.
During an instance initialization, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers: ...