是否[保护和/或内部]被避免? [英] Is [protected and/or internal] to be avoided?
问题描述
刚刚进入OOP ......
我记得曾经在某个地方读过保护和/或内部成员
变量通常是要避免的,或者可能是b $ b变量。坏事因为拥有这些成员的
违反了封装的OOP原则。它呈现的方式就像是,受保护的成员意味着基类和任何派生类之间的信任度很高 - 因为
派生类对受保护的
变量有无限制或不受控制的访问权限。
我现在有一种情况,我有一些
的成员变量和方法需要由派生类按原样调用。 - 所以没有明显需要[显然
对我来说]使它们抽象或虚拟,因为派生类具有
无需修改它们的行为(说到方法)特别)。
这似乎是对受保护范围的合理使用。我错过了什么?b $ b错过了什么?或者你们大多数人都会耸耸肩然后说非常
那么好。
谢谢。
Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation. The way it
was presented was something like, "having a protected member implies a high
degree of trust between the base class and any derived classes - because the
derived classes have unrestricted or uncontrolled access to the protected
variable."
I now have a situation where I have a few member variables and methods that
need to be called by derived classes "as is" - so no apparent need [apparent
to me anyway] to make them abstract or virtual as the derived classes have
no need to modify their behavior (speaking of the methods in particular).
This seems to be a perfectly reasonable use of ''protected'' scope. Am I
missing something? Or would most of you just shrug this off and say "very
well then."
Thanks.
推荐答案
Jeremy S.< A@B.comwrote:
Jeremy S. <A@B.comwrote:
刚进入OOP ...... >
我记得在某个地方读过保护和/或内部成员
变量通常是要避免的,或者可能是坏事。因为拥有这些成员的
违反了封装的OOP原则。它呈现的方式就像是,受保护的成员意味着基类和任何派生类之间的信任度很高 - 因为
派生类对受保护的
变量有无限制或不受控制的访问权限。
我现在有一种情况,我有一些
的成员变量和方法需要由派生类按原样调用。 - 所以没有明显需要[显然
对我来说]使它们抽象或虚拟,因为派生类具有
无需修改它们的行为(说到方法)特别)。
这似乎是对受保护范围的合理使用。我错过了什么?b $ b错过了什么?或者你们大多数人都会耸耸肩,然后说出非常好的b $ b。
Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation. The way it
was presented was something like, "having a protected member implies a high
degree of trust between the base class and any derived classes - because the
derived classes have unrestricted or uncontrolled access to the protected
variable."
I now have a situation where I have a few member variables and methods that
need to be called by derived classes "as is" - so no apparent need [apparent
to me anyway] to make them abstract or virtual as the derived classes have
no need to modify their behavior (speaking of the methods in particular).
This seems to be a perfectly reasonable use of ''protected'' scope. Am I
missing something? Or would most of you just shrug this off and say "very
well then."
一般来说保护/内部成员是完全合理的
- 但是(IMO)不是变量。内部属性,方法和事件很好 - 但是我总是将变量保密。
-
Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget 博客: http://www.msmvps.com /jon.skeet
如果回复小组,请不要给我发邮件
It''s perfectly reasonable to have protected/internal members in general
- but (IMO) not variables. Internal properties, methods and events are
fine - but I always keep variables private.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
2007年9月18日星期二10 :45:31-707,Jeremy S. < A@B.comwrote:
On Tue, 18 Sep 2007 10:45:31 -0700, "Jeremy S." <A@B.comwrote:
>刚刚进入OOP ......
我记得在某个地方读过有保护和/或内部成员的地方通常可以避免变量,或者可能是坏事。因为有这样的成员违反了OOP的封装原则。
>Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation.
你的来源是正确的。
Your source was correct.
>它呈现的方式是什么比如,拥有受保护的成员意味着基类和任何派生类之间的信任程度很高 - 因为
派生类具有对受保护的变量的无限制或不受控制的访问权限。 ;
>The way it
was presented was something like, "having a protected member implies a high
degree of trust between the base class and any derived classes - because the
derived classes have unrestricted or uncontrolled access to the protected
variable."
一个很好的理由。
A good reason.
>
我现在有一种情况,我有一些成员变量和方法需要由派生类按原样调用。 - 所以没有明显需要[显然对我来说]使它们抽象或虚拟,因为派生类没有必要修改它们的行为(特别是谈到这些方法)。
这个似乎是对受保护范围的合理使用。我错过了什么吗?或者你们大多数人都会耸耸肩,然后说非常好。
谢谢。
>
I now have a situation where I have a few member variables and methods that
need to be called by derived classes "as is" - so no apparent need [apparent
to me anyway] to make them abstract or virtual as the derived classes have
no need to modify their behavior (speaking of the methods in particular).
This seems to be a perfectly reasonable use of ''protected'' scope. Am I
missing something? Or would most of you just shrug this off and say "very
well then."
Thanks.
那里使方法受保护或内部是没有问题的。
对于你的成员变量,将它们设为私有并设置一个
protected / internal属性以允许访问。
rossum
There is no problem at all with making methods protected or internal.
For your member variables make them private and set up a
protected/internal property to allow access.
rossum
" Jeremy S." < A@B.com在留言中写道
news:e
"Jeremy S." <A@B.comwrote in message
news:e
这篇关于是否[保护和/或内部]被避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!