本文介绍了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.

推荐答案

不,私人会员 没有继承 因为私有成员的范围仅限于到定义它的类。仅继承public和protected成员。

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.

来自,

子类 不会继承其父类的
私有成员

但是,如果超类具有公共
或用于访问其
私有字段的受保护方法,则子类也可以使用
。嵌套类具有
访问
的所有私有成员的封闭类 - 两个字段和
方法。因此,
子类继承的public或
保护的嵌套类可以间接访问超类的私有成员的所有

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

来自,

一个有用的链接:

这篇关于Java继承中的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:17