Hello I was wondering if there is a way to store KClass objects where the class inherits from an interface. In my case I want to store the class in an enum. I was also wondering if there is a way to do something like an anonymous class, where i can just write the class definition in the enum instance.
enum class Pose(val poseClass: KClass<PluginPose>) {
// Todo: how to store anonymous classes in enum
// STANDING(class {
//
// }),
// SITTING,
// LAYING,
CRAWLING(CrawlingClass::class)
}
class CrawlingClass : PluginPose {
override fun init() { }
override fun play(target: McPlayer) { }
override fun stop() { }
}
interface PluginPose {
fun init()
fun play(target: McPlayer)
fun stop()
}
I don’t want to store instances of the interface PluginPose, because AFAIK that’s not possible (or if it is it wouldn’t be very useful to me). Instead I want to store classes that inherit from PluginPose. When I try this code Intellij tells me that I can’t store CrawlingClass in the enum, even though it inherits from PluginPose. Is there something else that I need to type in the generic of KClass to get it to accept inherited classes?
The other thing is if there is a way to create anonymous classes (something like in Pose.STANDING) and write them directly in the enum, instead of having to write the class and then store it in the enum with Class::class. I know of object keyword, but that gives me instances, not the class itself.
Also I would be interested to know if this method is the best to do, it if it is possible at all. Is there a better way to store Classes (not instances) in a collection like this other than using an enum? I want to be able to do things like this:
fun something(pose: Pose) {
if (pose == Pose.CRAWLING)
pose.play()
}
Edit: To be more detailed on the purpose of storing the class in an enum, I want to do things like in the if statement above, but also anything else you can do with a class like call the constructor to make new instances and such (like Pose.CRAWLING.poseClass(...)
)