Answers for "kotlin sealed classes"

3

kotlin sealed classes

open class Color{
    class Red : Color()
    class Orange : Color()
    class Blue : Color()
}
fun eval(c: Color) =
        when (c) {
            is Color.Red -> println("Paint in Red Color")
            is Color.Orange -> println("Paint in Orange Color")
            is Color.Blue -> println("Paint in Blue Color")
            else -> println("Paint in any Color")
        }

fun main(args: Array<String>) {
    val r = Color.Red()
    eval(r)
}
Posted by: Guest on September-07-2021

Browse Popular Code Answers by Language