목록Kotlin (26)
Foggy day
Kotlin don't have static. But it have companion object. class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) Person.create("companion object text") } data class Person(val name: String, val registered: Instant = Instant.now()) { companion object { fun cre..
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, ..
If you declare a value of variable in constructor, you don't need add parameter when you create class. class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val person = Person("jinhan") println("person name : ${person.name}, registered : ${person.register..
Add open in front of class to extend class in kotlin. class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val people = People("jinhan", 30) println("name : ${people.name}, age : ${people.age}") } class People(name: String, age: Int) : Person(name, age) {..
1. class Person constructor(name: String) { val name: String init { this.name = name } } 2. class Person constructor(name: String) { val name: String = name } 3. class Person constructor(val name: String) { } 4. class Person(val name: String) { }
It is about initialization of lazy. class KotlinPlayGroundActivity : AppCompatActivity() { val name: String by lazy { getName() } val name2: String by lazy(::getName) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) println("hello") println("name 1 : $name") println("name 2 : $name2") } @JvmName("getName1..