posted 24 years ago
I have the following general program. I am trying to
get the child classes to be dynamicallyd determined on
a switch statement.
The BuildScores method expects a BaseCharacterClass which is the parent of class1, class2, etc.. I thought that since parent is abstract, the class type would trickle down the hierarchy until it gets to the correct class.
For example, if case 0: , then it would make a class1 object. Then BuildScores method is called - sending a class1 object. Now in this method - the compiler would say Ok, I have a BaseCharacter abstract parent, now let me find the class1 and execute its methods.
Without this set-up I would have to call a different method for each class - which considering the amount of calculations that I am doing would be cumbersome. [Note the example below is only a small chunk of the actual code]
How does one solve this?
** code begins **
abstract BaseClass
class 1 extends BaseClass
class 2 extends BaseClass
etc...
//AnotherClass.java
public AnotherClass class
{
public static int[] GetScores(int i)
{
switch(i)
{
case 0:
class1 aClass = new class1();
return AbilityScores= BuildScores(aClass);
case 1:
class2 bClass =
new class2();
return abilityScores = BuildScores(bClass);
}
}
private static int[] BuildScores(BaseCharacterClass childClass)
{
int[] scores = {0,0}; //hold some ints
//currently it throwing run-time null errors
scores[0] = childClass.GetAInt();
scores[1] = childClass.GetAnotherInt();
}
}