c#类继承,这个设置的ID是哪个?

发布于 2024-10-26 03:03:39 字数 493 浏览 2 评论 0原文

/// <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

在你怀里撒娇 2024-11-02 03:03:39

您隐式使用 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.

来日方长 2024-11-02 03:03:39

它隐藏基类的成员并设置子类的ID。

It hides the base class' member and sets the child class' ID.

歌入人心 2024-11-02 03:03:39

您可能会发现,如果您构建它,您会收到警告,表明您有一个成员“隐藏”或“隐藏”另一个成员,但我不认为(至少默认情况下)它将标记为硬编译器错误。

要启用此方案以免造成混淆,您应该将基类中的 ID 属性标记为“虚拟”,并在子类中“覆盖”它:

public class ArtworkDataLabel : ArtworkLabel
{
    public string Data { get; set; }
    public override int ID { get; set; }

    public ArtworkDataLabel()
    {
        this.ID = 5;
    }
}    

public class ArtworkLabel
{
    public virtual  int ID { get; set; }
}

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:

public class ArtworkDataLabel : ArtworkLabel
{
    public string Data { get; set; }
    public override int ID { get; set; }

    public ArtworkDataLabel()
    {
        this.ID = 5;
    }
}    

public class ArtworkLabel
{
    public virtual  int ID { get; set; }
}
云朵有点甜 2024-11-02 03:03:39

这里发生的情况是,在 ArtworkDataLabel 中声明的 ID 隐藏了ArtworkLabel 中声明的 ID >。如果您检查 Visual Studio,您会发现这会生成一个警告。如果您打算隐藏该属性,可以使用new关键字,如下所示:

public new int ID {get;set;}

这里是一篇文章更详细地解释了此功能。

希望这有帮助:)

what happens here is that the ID declared in ArtworkDataLabel hides the ID declared in ArtworkLabel. If you check visual studio, you'll see that this generates a warning. If you intend to hide the that property, you can use the new keyword as follows:

public new int ID {get;set;}

here's an article that explains this feature in more detail.

Hope this helps :)

葮薆情 2024-11-02 03:03:39

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.

挽袖吟 2024-11-02 03:03:39

是的,您应该重命名它们或使用“新”修饰符。您获得的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文