c#类继承,这个设置的ID是哪个?
/// <summary>
/// Holds data associated with a label
/// </summary>
public class ArtworkDataLabel : ArtworkLabel
{
public string Data { get; set; }
public int ID { get; set; }
public ArtworkDataLabel()
{
this.ID = 5;
}
}
/// <summary>
/// A template label
/// </summary>
public class ArtworkLabel
{
public int ID { get; set; }
snip
}
这让我很困惑,为什么编译器允许这样做,以及设置了哪个 ID?如何区分 ID?我应该将其中一个 ID 重命名为其他名称吗?
/// <summary>
/// Holds data associated with a label
/// </summary>
public class ArtworkDataLabel : ArtworkLabel
{
public string Data { get; set; }
public int ID { get; set; }
public ArtworkDataLabel()
{
this.ID = 5;
}
}
/// <summary>
/// A template label
/// </summary>
public class ArtworkLabel
{
public int ID { get; set; }
snip
}
This confuses me, why is the compiler allowing this, and which ID is being set? How do I diferentiate between ID's? Should I be renaming one of the ID's to something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您隐式使用 new 运算符将该属性隐藏在基类中:
public new int ID { get;放; }
它重写了该方法。
You are hiding the property in the base class, implicitly using the new operator:
public new int ID { get; set; }
Which overrides the method.
它隐藏基类的成员并设置子类的ID。
It hides the base class' member and sets the child class' ID.
您可能会发现,如果您构建它,您会收到警告,表明您有一个成员“隐藏”或“隐藏”另一个成员,但我不认为(至少默认情况下)它将标记为硬编译器错误。
要启用此方案以免造成混淆,您应该将基类中的 ID 属性标记为“虚拟”,并在子类中“覆盖”它:
You will probably find if you build it that you have warning which indicates you have one member which 'hides', or 'shadows' another, but I don't think (by default, at least) it will flag up as a hard compiler error.
To enable this scenario so there is no confusion, you should mark the ID property in the base class as 'virtual', and in the subclass you 'override' it:
这里发生的情况是,在
ArtworkDataLabel
中声明的ID
隐藏了在ArtworkLabel
中声明的ID
>。如果您检查 Visual Studio,您会发现这会生成一个警告。如果您打算隐藏该属性,可以使用new
关键字,如下所示:这里是一篇文章更详细地解释了此功能。
希望这有帮助:)
what happens here is that the
ID
declared inArtworkDataLabel
hides theID
declared inArtworkLabel
. If you check visual studio, you'll see that this generates a warning. If you intend to hide the that property, you can use thenew
keyword as follows:here's an article that explains this feature in more detail.
Hope this helps :)
ArtwordDataLabel.ID 隐藏了 ArtworkLabel.ID,您应该会看到编译器警告您正在发生这种情况,并且您应该使用“new”来表明这是故意的。通过将属性更改回传统属性并包括一些跟踪日志记录,观察行为非常容易。
我认为这里更大的问题是继承是否是这些类的正确结构。根据您尝试对这些类执行的操作,您可能会发现使用装饰器模式甚至将它们完全分开更合适。
ArtwordDataLabel.ID hides ArtworkLabel.ID and you should be seeing a compiler warning you that this is happening and that you should use "new" to indicate that this is intentional. It's really easy to observe the behavior by changing the properties back to traditional properties and including some trace logging.
I think the bigger question here though is whether inheritance is the right structure for these classes. Depending on what you're trying to do with these classes you may find that using the decorator pattern or even keeping them completely separate is more appropriate.
是的,您应该重命名它们或使用“新”修饰符。您获得的 ID 始终是继承类中的 ID,而不是父类的 ID。
Yes, you should rename them or use the 'new' modifier. The ID that you're getting is always the one in the inheriting class, not the parent's.