public class Schedule_Action : MonoBehaviour
{
public List<Action> mondaySchedule = new List<Action>();
public virtual List<Action> MondaySchedule
{
get { return mondaySchedule; }
}
}
public class Schedule_ActionHire : Schedule_Action
{
//causes an error here saying it should match overriden with Action
public override List<Action_Adventure> MondaySchedule
{
get
{
return mondaySchedule.Cast<Action_Adventure>().ToList();
}
}
}
'Action_Adventure'是"动作"的孩子。
有没有办法绕过错误?或者,也许用与上述代码给出的逻辑相同的另一种方式?
您无法更改成员的签名。
但是使用new
您可以在基类中隐藏成员:
public class A
{
// no 'virtual' here
public string Value { get; set; }
}
public class B : A
{
public new int Value { get; set; }
}
但是,这种方法可能会令人困惑。
相反,您可以执行以下操作:源自Action
并添加一种抽象方法,该方法将以不同的方式处理内容:
public class Action
{
}
public class ActionAdventure : Action
{
}
public class Base
{
private readonly List<Action> _actions = new List<Action>();
public List<Action> Actions
{
get { return _actions; }
}
// call this from your code
protected virtual void HandleActions()
{
foreach (var action in Actions)
{
}
}
}
public class Derived : Base
{
protected override void HandleActions()
{
var adventures = Actions.OfType<ActionAdventure>();
foreach (var adventure in adventures)
{
}
}
}