为了简单起见,我有两个类:ActionTaken和MovementTaken. MovementTaken是一个派生类ActionTaken。我有一个Queue完整的动作,每个动作都有一个char决定动作类型的。(每个动作都有一个正确的类型)我想将 的元素传递Queue给一个函数,该函数专门与一个MovementTaken参数一起工作,但是由于我使用多态性,所以参数是 type ActionTaken,但是我不能使用来自 的成员变量MovementTaken,但是不要不存在于ActionTaken. 但是如果我将 functionactivateMovement的参数设置为 type MovementTaken,我相信会有一个错误,说你不能将基类型转换为派生类型。这是代码:
public abstract class ActionTaken : MonoBehaviour
{
public char type;
public Transform minionTakingAction;
}
public class MovementTaken : ActionTaken
{
public int targetTileH;
public int targetTileV;
public MovementTaken(Transform _minionTakingAction, int _targetTileH, int _targetTileV)
{
type = 'M';
minionTakingAction = _minionTakingAction;
targetTileH = _targetTileH;
targetTileV = _targetTileV;
}
}
Queue<ActionTaken> actionTaken;
public void activateMovement(ActionTaken toActivate)
{//some code using toActivate's members targetTileH and targetTileV}
眼眸繁星
FFIVE
相关分类