在C#代码中,如果Rebar
类派生自Reinforcement
类并且RebarShape
类继承ReinforcementShape
类。是否可以使用RebarShape
类替代基类中的属性ReinforcementShape
?用派生类替代基类的属性
public class ReinforcementShape
{
}
public class RebarShape : ReinforcementShape
{
}
public class Reinforcement
{
public ReinforcementShape Shape { get; set; }
}
public class Rebar : Reinforement
{
// I want to override the Shape property
// but with its derived class which is RebarShape
// override the base property somehow!
public RebarShape Shape { get; set; }
}
更新:
有什么不对当前的实现?
在基地:
public virtual ReinforcementShape Shape { get; set; }
在得出:
public new RebarShape Shape { get; set; }
您无法重写某个属性并将其更改为返回类型。 –