如何隐藏在一个类继承的属性,而无需修改继承的类(基类)? [英] How to hide an inherited property in a class without modifying the inherited class (base class)?

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

问题描述

如果我有以下code例如:

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

我应该怎么做来隐藏属性名称无需修改(不显示为成员的ClassA的成员) ClassBase

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

推荐答案

我在这里闻到了code气味。在我看来,如果你实现所有的基类的功能,你要继承一个基类。你在做什么并没有真正重新present面向对象的原则,正确。因此,如果你想从你的基地,你应该实现名称继承,否则,你有你的继承走错了路周围。你们班应该是你的基类,当前的基类应该从继承,如果这就是你想要什么,而不是周围的其他方式。

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.

如果您试图过时的财产(和它的基类的虚拟声明),那么你可以要么使用过时的属性就可以了:

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

编辑:布赖恩在评论中指出,该属性的第二个参数会导致编译器错误,如果有人引用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();
    }
}

您仍然可以使用过时的属性相同的方式犹如方法重写,或者你可以抛出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天全站免登陆