Java继承中的私有成员

我被告知,对于Java子类,它可以继承其超类的所有成员.这甚至意味着私人会员呢?我知道它可以继承受保护的成员.

谁可以给我解释一下这个.我现在完全糊涂了.

解决方法:

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

Java Documentation,

Private Members in a Superclass

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.

JLS开始,

Members of a class that are declared
private are not inherited
by
subclasses of that class. Only members
of a class that are declared protected
or public are inherited by subclasses
declared in a package other than the
one in which the class is declared.

一个有用的链接:Does subclasses inherit private fields?

上一篇:类的加载过程 以及实例的加载顺序


下一篇:(0)HarmonyOS鸿蒙开发工具DevEco Studio工程文件目录结构