我如何在Kotlin的运行时获取接口的所有实现

时间:2018-12-22 21:05:39

标签: reflection kotlin

我知道我可以在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) }
}

0 个答案:

没有答案