I created an abstract class as follows:
abstract class Chance
{
public void setTeams(SportEvent aSportEvent)
{
firstTeam = aSportEvent.getFirstTeam();
secondTeam = aSportEvent.getSecondTeam();
}
private int totalPlayedGames()
{
int playedAtHome = firstTeam.getHomePlayedGames();
int playedAway = secondTeam.getAwayPlayedGames();
int playedGames = playedAtHome + playedAway;
return playedGames;
}
private int totalScoredGoals()
{
int homeScoredGoals = firstTeam.getHomeScoredGoals();
int awayScoredGoals = secondTeam.getAwayScoredGoals();
int scoredGoals = homeScoredGoals + awayScoredGoals;
return scoredGoals;
}
abstract double getChance()
Team firstTeam;
Team secondTeam;
}
Then, of course, I have several classes with the method getChance()
...
Now, I wanted to create a class in which I can get the result of getChance()
of these new classes. I thought to use something like:
Class aClass = Class.forName(chanceClass....);
Object obj = aClass.newInstance();
and now I would like to use the 'getChance()' method.
How can I do it?
You can simply cast it:
Chance obj = (Chance) aClass.newInstance();
double d = obj.getChance();
Without casting:
Class<? extends Chance> aClass = Class.forName(chanceClass....).asSubclass(Chance.class);
Chance obj = aClass.newInstance();
double d = obj.getChance();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.