Am I missing something key here?
A
is something which extends Action, ac
is also something which extends Action. However this is a bit like the facility
Yesterday I saw someone.
Someone is the President
Therefor, yesterday I was the President.
The problem is the compiler has no may of knowing that the A
and the Action
are compatible sub-classes of Action
A simple work around is to not be so specific. Use Action
instead of A
in this method.
public List<Action> getActionsFor(Actor a) {
// this has to return a copy of the List or you will be modifying an original
List<Action> theActions = super.getActionsFor(a);
for (Action ac: a.getItemCarried().getActions()) {
theActions.add(ac); // ac is an Action so all good.
}
return theActions;
}
OR you could do what you had, but more complicated
public <A extends Action> List<A> getActionsFor(Actor<A> a) {
List<A> theActions = super.getActionsFor(a);
for (A ac: a.getItemCarried().getActions()) {
theActions.add(ac); // ac is an A, so all good.
}
return theActions;
}