Foggy day

Kotlin - destructing 본문

Kotlin

Kotlin - destructing

jinhan38 2021. 3. 16. 21:23

If you use destructing, you can access to object properties much easier.

 

class KotlinPlayGroundActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_play_ground)

        val persons = listOf(Person("jinhan"), Person("jinhan2"), Person("jinhan3"))
        show(persons)
    }

    data class Person(val name: String, val registered: Instant = Instant.now())

    fun show(persons: List<Person>) {
        for ((name, date) in persons) {
            println("$name's registration date : $date")
        }
    }

}

 

 

'Kotlin' 카테고리의 다른 글

Kotlin - Singleton  (0) 2021.03.16
Kotlin - companion object  (0) 2021.03.16
Kotlin - data class  (0) 2021.03.15
Kotlin - class extension  (0) 2021.03.15
Kotlin - constructor  (0) 2021.03.15