I am trying to add cards to a list by using their class name, then I want to call one of their methods. How can I do that? I have this for now:
Into the super class Card:
public static ArrayList<Object> CreateAllCards() throws ClassNotFoundException
{
ArrayList<Object> cardArray = new ArrayList<>();
for (int n = 0; n<cardList.length; n++)
{
if (isCardAllowedToBeCreated[n])
{
Class card = Class.forName(cardList[n]);
cardArray.add(card);
}
}
return cardArray;
}
static String[] cardList = {
"CrtAbyssalSpecter",
"CrtAirElemental",
"ArtAladdinsRing",
"SorAmbitionsCost",
"CrtAnabaShaman",
"CrtAngelOfMercy",
"CrtAngelicPage",
//...
};
}
Somewhere else, my main:
public static void main(String[] args) throws ClassNotFoundException {
ArrayList<Object> cardList = Card.CreateAllCards();
for (int n=0; n<cardList.size(); n++)
{
if (cardList.get(n) instanceof Creature)
{
// How to call a specific method?
}
}
}
Also, is there a way to directly create an arraylist of Card by using each card name rather than creating an arraylist of object (by each card name) like I do?