在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; }