jeudi 15 décembre 2022

Kotlion: invoke same overridden function in all sealed subclasses that implements same interface

I'm trying to invoke same function that in the subclasses the implements same interface. Or the question could be how to invoke function in KClass<out Drink>? Is it possible or is there any work around?

fun main() {
    println(Tea().getOptimalTemp())
    println(Wine().getOptimalTemp())
    val list = listOf(Drink::class.sealedSubclasses)
    for(drink in list){
        println(drink.getMethods.getOptimalTemp())
    }
}

interface Drink {
    fun getOptimalTemp(): Int
}

class Tea : Drink {
    override fun getOptimalTemp():Int{
        return 60
    }
}

class Wine : Drink {
    override fun getOptimalTemp():Int{
        return 10
    }
}

I expect to add more class implements that interface in the future without changing the loop.





Aucun commentaire:

Enregistrer un commentaire