这里私人成员的价值是什么?
如果我有两个类 A
和 B
,则 B
inehrits A
。换句话说,A
是基类,B
是派生类。
现在,假设类 A
具有 private
成员。由于类B
继承了类A
,私有数据成员成为类B
的一部分。
众所周知,私有成员即使被派生类也不能访问,但是,它们现在位于派生类中。在这种情况下,隐私的价值是什么?
谢谢。
If I have two classes A
and B
, such that B
inehrits A
. In other words, A
is the base class, and B
is the derived class.
Now, suppose that class A
has private
members. Since class B
inherited class A
, the private data members became part of class B
.
As we know, private members are said not to be accessed even by derived classes, but, they are now in the derived class. What is the value of privacy in this case?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
B
的成员函数无法以任何方式使用它们,但它们对于A
的成员函数很有用,而B
又对A
的成员函数有用会依赖。它只是另一个级别的封装(B
不应该关心A
的功能是如何实现的)。They aren't usable in any way by member functions of
B
, but they will be useful to member functions ofA
, whichB
in turn will rely on. It's simply another level of encapsulation (B
shouldn't care how the functionality ofA
is implemented).好吧,因为它们是 A 类私有的,所以我假设 A 类的实例将它们用于某些用途。如果没有,可以将其删除。
Well, since they are private to class A, I would assume instances of class A use them for something or other. If not, they can be removed.
“private”的目的是告诉编写B类的开发人员不应该访问这个成员。
A 类的开发人员以某种方式使用该成员,而 B 类的开发人员无需关心。
The intention of "private" is to tell the developers who write the class B that they should not access this member.
The developer of class A uses the member a certain way that class B's developers do not need to concern themselves with.
B 对象的 A 成员将是您的对象需要它们的任何内容,以便表示您用它表示的内容。 A 类的方法可能会使用它们,并且您可能会调用一些 A 类方法。实际上,您应该只关心 A 的公共(或受保护)接口,并利用它。
例如:如果 A 是具有写入和读取方法的 TCP/IP 协议套接字实现,则 B 可能是 A 之上的 SSL 实现。您(基本上)需要重新定义读取和写入,以便进行加密,然后使用 A ::read 和 A::write 来对套接字进行实际的写入和读取。在这个设计中你根本不需要知道你的socket(或者TCP协议细节)是什么,所以A为你抽象了它,你无权访问它。
The A members of the B objects will be whatever your object needs them to be in order to represent what you're representing with it. A class' methods probably uses them, and you'll probably call some A class methods. You should only care about A's public (or protected) interface, really, and make use of it.
e.g: if A is a TCP/IP protocol socket implementation which has write and read methods, B might be a SSL implementation on top of A. You'd (basically) redefine read and write so you do your encryption, and then use A::read and A::write to do your actual writing and reading from/to the socket. You have no need whatsoever to know what your socket (or the TCP protocol details) is in this design, so A abstracts it for you, and you have no access to it.
它将保留 - 私人成员。
不知道你在哪里看到过不能访问的?
It will stay - a private member.
I don't know where you've seen that they are not to be accessed?
因为,无论您相信与否,您都不想将 B 与 A 的实现细节结合起来。想象一个最简单的使用场景:
现在,让我们假设需要对您的产品进行更改,而现在 A 不需要有一个 x 变量,但取而代之的是获取 x 的值,该值以其他方式(可能是组合)表示。由于 B 直接访问该成员,因此您必须更改您创建的任何 B,以通过任何尚未到位的新机制来获取 x。你需要改变的越多,出现问题的空间就越大。
如果您改为创建 xa 私有成员并提供受保护的 getter,则不会遇到此问题。
再说一遍,这是最简单的例子。一般来说,像 x 这样的值用于 A 行为的实现,而 B 不应该关心行为是如何实现的,也不应该使用它。
Because, believe it or not, you don't want to couple B to the implementation details of A. Imagine a scenario with the simplest kind of use:
Now, lets assume that a change to your product is required and now A doesn't have an x variable but instead fetches the value x meant to represent in some other way, composition maybe. Since B accesses the member directly you have to change any B that you've created to grab x through whatever new mechanism is not in place. The more you have to change the more room for problems there is.
If you'd instead made x a private member and provided a protected getter you'd not have this problem.
Again, this is the simplest example. In general values like x are used in the implementation of A's behavior and B should not be concerned with how the behavior is implemented nor should it have access to muck with it.