Foggy day

Kotlin - Object Expressions 본문

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)

 

'Kotlin' 카테고리의 다른 글

Kotlin - coroutine Scope basic  (0) 2021.04.10
Kotlin - data class destructuring  (0) 2021.04.08
Kotlin - when, for  (0) 2021.04.06
Kotlin - recursive function -2 : TCE  (0) 2021.03.21
Kotlin - recursive function -1  (0) 2021.03.21