I have an abstract class Command. The constructor looks like this:
public Command(String cmd, String helpMsg, String... args) {
this.command = cmd;
this.helpMsg = helpMsg;
this.args = args;
}
Whenever a certain condition is met, I want to be able to print out each command, it's help message and arguments.
If I could loop through the instances of the subclasses of Command, I could call the getter functions for these attributes. However, I don't know how I could store instances like this. I've been reading about generics but haven't been able to solve this problem yet.
Right now I have another class with this code:
private static Map<Class<? extends Command>, ? extends Command> instances;
public static <T extends Command> void addInstance(Class<T> tClass, T tInstance) {
instances.put(tClass, tInstance);
}
But it gives me this error:
Required type: capture of ? extends Command
Provided: T
It would also be nice if I were able to get instances to individual subclasses with getInstance(subclass.class)