如何在不修改继承类(基类)的情况下隐藏类中的继承属性? [英] How to hide an inherited property in a class without modifying the inherited class (base class)?

查看:25
本文介绍了如何在不修改继承类(基类)的情况下隐藏类中的继承属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下代码示例:

If i have the following code example:

public class ClassBase
{
    public int ID { get; set; }

    public string Name { get; set; }
}

public class ClassA : ClassBase
{
    public int JustNumber { get; set; }

    public ClassA()
    {
        this.ID = 0;
        this.Name = string.Empty;
        this.JustNumber = string.Empty;
    }
}

如何在不修改ClassBase 的情况下隐藏属性Name(不显示为ClassA 成员的成员)?

What should I do to hide the property Name (Don't shown as a member of ClassA members) without modifying ClassBase ?

推荐答案

我在这里闻到了代码的味道.我的观点是,如果您要实现该基类的所有功能,您应该只继承该基类.你在做什么并没有真正正确地代表面向对象的原则.因此,如果你想从你的基础继承,你应该实现 Name,否则你的继承方式是错误的.你的类 A 应该是你的基类,如果这是你想要的,你当前的基类应该从 A 继承,而不是相反.

I smell a code smell here. It is my opinion that you should only inherit a base class if you're implementing all of the functionality of that base class. What you're doing doesn't really represent object oriented principles properly. Thus, if you want to inherit from your base, you should be implementing Name, otherwise you've got your inheritance the wrong way around. Your class A should be your base class and your current base class should inherit from A if that's what you want, not the other way around.

但是,不要偏离直接问题太远.如果您确实想无视规则"并想继续走您选择的道路 - 您可以这样做:

However, not to stray too far from the direct question. If you did want to flout "the rules" and want to continue on the path you've chosen - here's how you can go about it:

约定是实现该属性,但在调用该属性时抛出 NotImplementedException - 尽管我也不喜欢那样.但这是我个人的观点,并不能改变这个约定仍然存在的事实.

The convention is to implement the property but throw a NotImplementedException when that property is called - although, I don't like that either. But that's my personal opinion and it doesn't change the fact that this convention still stands.

如果您试图废弃该属性(并且它在基类中声明为虚拟的),那么您可以在其上使用 Obsolete 属性:

If you're attempting to obsolete the property (and it's declared in the base class as virtual), then you could either use the Obsolete attribute on it:

[Obsolete("This property has been deprecated and should no longer be used.", true)]
public override string Name 
{ 
    get 
    { 
        return base.Name; 
    }
    set
    {
        base.Name = value;
    }
}

( 正如 Brian 在评论中指出的那样,如果有人引用 Name 属性,该属性的第二个参数将导致编译器错误,因此他们甚至无法使用它虽然你已经在派生类中实现了它.)

( As Brian pointed out in the comments, the second parameter of the attribute will cause a compiler error if someone references the Name property, thus they won't be able to use it even though you've implemented it in derived class.)

或者正如我提到的使用 NotImplementedException:

Or as I mentioned use NotImplementedException:

public override string Name
{
    get
    {
        throw new NotImplementedException();
    }
    set
    {
        throw new NotImplementedException();
    }
}

但是,如果属性声明为虚拟,那么您可以使用 new 关键字来替换它:

However, if the property isn't declared as virtual, then you can use the new keyword to replace it:

public new string Name
{
    get
    {
        throw new NotImplementedException();
    }
    set
    {
        throw new NotImplementedException();
    }
}

您仍然可以像重写方法一样使用 Obsolete 属性,或者您可以抛出 NotImplementedException,无论您选择哪种方式.我可能会使用:

You can still use the Obsolete attribute in the same manner as if the method was overridden, or you can throw the NotImplementedException, whichever you choose. I would probably use:

[Obsolete("Don't use this", true)]
public override string Name { get; set; }

或:

[Obsolete("Don't use this", true)]
public new string Name { get; set; }

取决于它是否在基类中被声明为虚拟.

Depending on whether or not it was declared as virtual in the base class.

这篇关于如何在不修改继承类(基类)的情况下隐藏类中的继承属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆