【发布时间】:2017-07-05 06:17:00
【问题描述】:
在 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; }
【问题讨论】:
-
你不能覆盖一个属性并改变它的返回类型。
标签: c# inheritance properties polymorphism