I have interface Cat that is implemented by classes LolCat and FatCat. My program needs to remember which type of Cat user wanted melt with next. I cannot instantiate the proper Cat yet, so I need to remember the class. How do I do this?
I tried:
Class nextCatClass = FatCat.class;
But now I don't know how to instantiate it using a variable. new nextCatClass() is not a valid syntax. I also tried
Class<Cat> nextCatClass = FatCat.class;
but now I get an "incompatible types" error.
Risking to annoy the heck out of Java users, here's what I'd do in Python:
next_cat_class = FatCat
...
instance = next_cat_class()
"FatCat"or"LolCat"in your list, and then doif(catType == "FatCat") { return new FatCat(); } else { return new LolCat(); }? (Of course you may want to use enumerated types, etc., but just something using this principle?)Class<Cat> nextCatClass = FatCat.class;read: stackoverflow.com/questions/2745265/…