Kotlin
Kotlin - Object Expressions
jinhan38
2021. 4. 8. 23:38
val value = object {
val x = 0
val y = 10
val z = 20
}
println("value x : ${value.x}, y : ${value.y}, z : ${value.z}")
//// value x : 0, y : 10, z : 20
val school: School = object : School(3), ClassGroup {
override val students: Int
get() = super.students
override fun OnJoinClassGroup(classNumber: Int) {
super.OnJoinClassGroup(3)
}
}
println("school.students ${school.students}")
//// school.students 3
open class School(student: Int) {
open val students: Int = student
}
interface ClassGroup {
fun OnJoinClassGroup(classNumber: Int) {}
}
class KotlinPlayGroundActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kotlin_play_ground)
println("MakeHome : ${MakeHome.create()}")
}
}
class MakeHome {
companion object : Factory<Interior> {
override fun create(): Interior {
return Interior(1000000, 5)
}
}
}
interface Factory<T> {
fun create(): T
}
data class Interior(var price: Int, var people: Int)