简体   繁体   English

kotlin 中的 init 块和构造函数有什么区别?

[英]What is the difference between init block and constructor in kotlin?

I have started learning Kotlin.我已经开始学习 Kotlin。 I would like to know the difference between init block and constructor .我想知道init block 和constructor之间的区别。 What is the difference between this and how we can use this to improve?这与我们如何使用它来改进之间有什么区别?

class Person constructor(var name: String, var age: Int) {
    var profession: String = "test"

    init {
        println("Test")
     }    
}

The init block will execute immediately after the primary constructor. init块将在主构造函数之后立即执行。 Initializer blocks effectively become part of the primary constructor. Initializer 块有效地成为主构造函数的一部分。 The constructor is the secondary constructor.构造函数是辅助构造函数。 Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks is executed before the secondary constructor body.对主构造函数的委托作为辅助构造函数的第一条语句发生,因此所有初始化程序块中的代码都在辅助构造函数主体之前执行。

Example例子

class Sample(private var s : String) {
    constructor(t: String, u: String) : this(t) {
        this.s += u
    }
    init {
        s += "B"
    }
}

Think you initialized the Sample class with认为你初始化了 Sample 类

Sample("T","U")

You will get a string response at variable s as "TBU" .您将在变量s处获得字符串响应为"TBU" Value "T" is assigned to s from the primary constructor of Sample class, and then immediately init block starts to execute it will add "B" to the variable."T"Sample类的主构造函数中赋值给s ,然后立即 init 块开始执行,它会将"B"添加到变量中。 After init block secondary constructor block starts to execute and s will become "TBU" .在 init 块后,二级构造器块开始执行, s将变为"TBU"

A class in Kotlin class a primary constructor (the one after a class name) which does not contain code, it is only able to initialize properties (eg class X(var prop: String) ). Kotlin 类中的一个类是一个不包含代码的主构造函数(类名后面的那个),它只能初始化属性(例如class X(var prop: String) )。

The init{..} block in the place, where you can put more code that will run after properties are initialized : init{..}块在该位置, 您可以在其中放置更多将在属性初始化 运行的代码

initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers初始化程序块的执行顺序与它们在类主体中出现的顺序相同,与属性初始化程序交错

More about that is inhttps://kotlinlang.org/docs/reference/classes.html#constructors有关这方面的更多信息,请参见https://kotlinlang.org/docs/reference/classes.html#constructors

Here is an example:这是一个例子:



class X(var b: String) {
  val a = print("a")

  init {
    print("b")
  }

  constructor() : this("aaa") {
    print("c")
  }
}


X()

When called it prints abc .调用时会打印abc Thus the init{..} in invoked after primary constructor but before a secondary one.因此init{..}在主构造函数之后但在辅助构造函数之前调用。

Since,自从,

The primary constructor cannot contain any code.主构造函数不能包含任何代码。

https://kotlinlang.org/docs/reference/classes.html https://kotlinlang.org/docs/reference/classes.html

init blocks allow adding code to the primary constructor. init 块允许向主构造函数添加代码。

As stated in the Kotlin docs:如 Kotlin 文档中所述:

The primary constructor cannot contain any code .主构造函数不能包含任何代码 Initialization code can be placed in initializer blocks , which are prefixed with the init keyword.初始化代码可以放在以init关键字为前缀的初始化程序块中。

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: ...在实例初始化期间,初始化程序块的执行顺序与它们在类体中出现的顺序相同,并与属性初始化程序交错:...

https://kotlinlang.org/docs/classes.html#constructors https://kotlinlang.org/docs/classes.html#constructors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM