class Person constructor(firstName: String) { /*...*/ }
class Person(firstName: String) { /*...*/ }
class InitOrderDemo(name: String) {
val firstProperty = "First property: $name".also(::println)
init {
println("First initializer block that prints $name")
}
val secondProperty = "Second property: ${name.length}".also(::println)
init {
println("Second initializer block that prints ${name.length}")
}
}
📌 init 블록은 클래스가 생성되면 가장 먼저 실행된다.
class Person(val pets: MutableList<Pet> = mutableListOf())
class Pet {
constructor(owner: Person) {
owner.pets.add(this) // adds this pet to the list of its owner's pets
}
}
val invoice = Invoice()
val customer = Customer("Joe Smith")