简体   繁体   中英

How to get a list of classes that extend from a base class in Kotlin?

consider following classes structure in Kotlin:

open class Base {
   ...
}

class A : Base() {
   ...
}

class B : Base() {
   ...
}

...

class Z : Base() {
   ...
}

How would I go around getting a list of ALL classes ( A - Z in this particular example) that inherit from Base class?

I tried using reflection with BaseTest::class.nestedClasses and other reflection properties but they don't seem to be finding number of extending classes properly.

You could define your Base class as sealed class Base and then access Base::class.sealedSubclasses

Information about sealed classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM