Java继承中的私有成员
有人告诉我,对于 Java 子类,它可以继承其超类的所有成员。那么这是否意味着私人成员也是如此?我知道它可以继承受保护的成员。
有人可以向我解释一下吗?我现在完全困惑了。
I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.
Can someone explain this to me. I am now totally confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,私有成员不会被继承,因为私有成员的范围仅限于到定义它的类。只有 public 和 protected 成员才会被继承。
来自
Java 文档
,来自
JLS
,一个有用的链接:子类继承私有字段吗?
No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited.
From the
Java Documentation
,From the
JLS
,A useful link : Does subclasses inherit private fields?
这取决于您对“继承”一词的确切用法。我将通过示例进行解释。
假设您有两个类:
Parent
和Child
,其中Child
扩展了Parent
。此外,Parent
有一个名为value
的私有整数。现在问题来了:
Child
是否继承了私有value
?在 Java 中,继承的定义方式是“否”。然而,在一般的 OOP 术语中,存在轻微的歧义。您可以说它没有继承,因为
Child
无法显式引用value
。即任何像this.value
这样的代码都不能在Child
中使用,也不能在某些调用代码中使用obj.value
(显然) 。然而,从另一种意义上来说,您可以说
value
是继承的。如果您认为Child
的每个实例也是Parent
的实例,则该对象必须包含value
作为在Parent
中定义。即使Child
类对此一无所知,名为value
的私有成员仍然存在于Child
的每个实例中。因此,从这个意义上说,您可以说value
是在Child
中继承的。因此,在不使用“继承”这个词的情况下,只需记住子类不知道父类中定义的私有成员。但还要记住,这些私有成员仍然存在于子类的实例中。
This kind of depends on your exact usage of the word inheritance. I'll explain by example.
Suppose you have two classes:
Parent
andChild
, whereChild
extendsParent
. Also,Parent
has a private integer namedvalue
.Now comes the question: does
Child
inherit the privatevalue
? In Java, inheritance is defined in such a way that the answer would be "No". However, in general OOP lingo, there is a slight ambiguity.You could say that it not inherited, because nowhere can
Child
refer explicitly tovalue
. I.e. any code likethis.value
can't be used withinChild
, nor canobj.value
be used from some calling code (obviously).However, in another sense, you could say that
value
is inherited. If you consider that every instance ofChild
is also an instance ofParent
, then that object must containvalue
as defined inParent
. Even if theChild
class knows nothing about it, a private member namedvalue
still exists within each and every instance ofChild
. So in this sense, you could say thatvalue
is inherited inChild
.So without using the word "inheritance", just remember that child classes don't know about private members defined within parent classes. But also remember that those private members still exist within instances of the child class.
在这里您将百分百满意。我在我的电脑上测试了它,我的结论是我将把它发布在这里。只需浏览下面编写的程序,查看程序输出并阅读最后给出的结论即可。
要亲自测试它,请复制整个程序并将其保存在名为“InheritanceTest.java”的文件中,然后编译并最终运行它。
程序
输出
结论
是的,子类可以间接访问超类的私有成员。子类不能直接访问超类的私有成员。
超类的所有公共、私有和受保护成员(即所有字段和方法)都由子类继承,但子类只能直接访问超类的公共和受保护成员。如果从超类继承的成员允许访问超类的私有成员,则子类可以使用此继承的成员来访问超类的私有成员。
You will be satisfied here 100%. I tested it on my computer and what I concluded I'm going to post it here. Just go through the program written below, see the program output and READ THE CONCLUSION given at the end.
To test it yourself, copy the whole program and save it in a file named "InheritanceTest.java" then compile it and finally run it.
Program
Output
Conclusion
Yes, a subclass can indirectly access the private members of a superclass. A subclass can't directly access the private members of a superclass.
All the public, private and protected members (i.e. all the fields and methods) of a superclass are inherited by a subclass but the subclass can directly access only the public and protected members of the superclass. If an inherited member from a superclass gives access to a private member of the superclass then the subclass can use this inherited member to access the private member of the superclass.
IMO 绝不是一个定义问题。在基于类的继承中,意味着将行为传播给后代。由于此类私有成员确实会被继承,因此我不会详细说明这是如何发生的。
实际上,我发现“不继承”的答案对于新开发人员来说是危险的,他们不会立即理解私有成员隐藏在你的类的皮肤下,它们(可以)对其行为、类的大小产生严重影响。 在计算机科学中,“开发先于理解”是很常见的,但是
,假设某些编写基于类的著名 OO 平台手册的技术人员采用了错误的“定义”,我们应该避免构建(或破坏)我们的 OOP 概念化。
很抱歉在这么旧的帖子中陈述一些内容,但这个问题总是有效的。
IMO by no way is it a matter of definition. In class-based Inheritance implies propagation of behavior to descendants. As such private members DO get inherited , and I will not go into the details how this happens.
Actually I find the "not inherited" answer to be dangerous for new developers and they do not comprehend right away that the private members are there hidden under the skin of your class and they (can) have severe impact on its behavior, size of the objects etc.
It is common that "development comes before understanding" in computer science, however lets avoid building (or destroying) our conceptualization of OOP assuming the wrong "definition" adopted by some technician writing the manual of a well known class based OO platform.
Sorry for stating something in such an old post, but the issue is always valid.
虽然 https://docs.oracle .com/javase/specs/jls/se8/html/jls-8.html#jls-8.2显示私有成员不被继承。实际上,它是被子类继承的。当我们使用调试器跟踪变量时,它会在“继承”标签下显示私有成员,所以试试吧。还有一个帖子讨论这个问题,大部分都认为不是遗传的,这误导了很多人,包括我一开始。
Though https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2 shows that Private Members are not inherited. Actually, it is inherited by subclass. When we use debuggers to trace variables, it will show the private members under the label of "inherited", so just try it. there is another post discussing this question, and most of them think not inherited, which misleads many people, including me at first.