我知道我可以在Kotlin中获得sealed class
的所有子类,但是我正在寻找一种获取接口的所有实现的方法。
所以不是...
sealed class HotDrinkFactory {
abstract fun prepare(amount: Int): HotDrink
}
class TeaFactory : HotDrinkFactory() {
override fun prepare(amount: Int): HotDrink {
...
}
}
class CoffeeFactory : HotDrinkFactory() {
override fun prepare(amount: Int): HotDrink {
...
}
}
fun main(args: Array<String>) {
val hotDrinkFactories = HotDrinkFactory::class.sealedSubclasses
hotDrinkFactories.forEach { println(it::class.qualifiedName) }
}
...我想要
interface HotDrinkFactory {
fun prepare(amount: Int): HotDrink
}
class TeaFactory : HotDrinkFactory {
override fun prepare(amount: Int): HotDrink {
...
}
}
class CoffeeFactory : HotDrinkFactory {
override fun prepare(amount: Int): HotDrink {
...
}
}
fun main(args: Array<String>) {
val hotDrinkFactories = HotDrinkFactory::class.<<< Something here? >>>
hotDrinkFactories.forEach { println(it::class.qualifiedName) }
}