如何从子类隐藏父属性 [英] how to hide parent property from child class

查看:118
本文介绍了如何从子类隐藏父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何隐藏子类中的父类属性。

How can I hide the parent class property in child class.

其中父类有一个名为Parent的属性,我不想使用它在儿童班。如何删除或隐藏它。

Where parent class has a property called "Parent", where I don't want to use that in child class. How can I remove or hide that.

推荐答案

所以听起来你问的是以下内容。你有

So it sounds like you are asking the following. You have

class Parent {
    public SomeType ParentProperty { get; set; }
}

class Child : Parent { }

和你想隐藏 SomeProperty Child 的实例中可见。

and you want to hide SomeProperty from being visible in instances of Child.

不要这样做!不要隐藏可见的基类属性。首先,它很容易到处:

Do not do this! Do not hide properties from the base class that are visible. First, it's easy to get around:

Parent p = new Child();
p.ParentProperty; // oops!

其次,它违反了利斯科夫替代原则。基本上,原则说任何你知道的关于 Parent 的所有实例的事情对于 Child 。在这里,我们知道 Parent 的所有实例都有一个名为 ParentProperty 的可见属性,类型为 SomeType 。因此,对于 Child 的所有实例,同样应该(道德应该)。

Second, it's a huge violation of the Liskov substitution principle. Basically, the principle says that anything that you know to be true about all instances of Parent should also be true about all instances of Child. Here, we know that all instances of Parent have a visible property called ParentProperty of type SomeType. Therefore, the same should (moral should) be true of all instances of Child.

你能告诉我们为什么你想这样做,也许我们可以建议一个替代方案?

Can you tell us why you want to do this and maybe we can suggest an alternative?

这篇关于如何从子类隐藏父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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