问题描述
是否有覆盖公共虚拟方法或属性,以便它在我的派生类中是私有的?$ / b
我尝试使用新的财产和私有,但没有运气。
-
------------------ -----------------
Ken Varn
高级软件工程师
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
--------------- --------------------
Is there anyway to override a public virtual method or property so that it
is private in my derived class?
I tried using new on the property and making it private, but no luck.
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
-----------------------------------
推荐答案
如果你是从一个类中派生出来的,那么你必须将
超类中的操作视为契约。不要被侵犯。你可以在超级类的实例上执行的任何操作,你必须能够在
子类的实例上做(Liskov原理)。
因此,你不能让继承的操作比在
超类中更受限制。
一点继承是多态性的可能性,你在
中有些情况下不知道实例是超类还是派生的
类。无论方法是否被覆盖,它仍然必须能够响应超类的操作,
。如果方法被覆盖(例如空方法),则取决于子类
,但它们仍然必须能够响应操作。
// Bjorn A
If you have derived from a class, you''ll have to see the operations in the
superclass as a "contract" not to be violated. Any operations you can do on
an instance of the superclass, you must be able to do on instances of the
subclasses (Liskov''s principle).
Hence, you cannot make an inherited operation more restricted than in the
superclass.
One point of inheritance is the possibility of polymorphism, where you in
some cases don''t know if the instance is of the superclass or a derived
class. It must still be able to respond to the operations of the superclass,
regardless if the methods are overridden or not. It''s up to the subclasses
if the methods are overridden (with e.g. "empty" methods), but they still
must be able to respond to the operation.
// Bjorn A
这篇关于如何在派生类中将公共虚方法重写为私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!