java 子类访问父类私有成员_关于java:如果两个类都在同一个包中,子类是否可以访问父类私有成员...

我正在读一个Java文档,上面写着

A subclass inherits all of the public and protected members of its

parent, no matter what package the subclass is in. If the subclass is

in the same package as its parent, it also inherits the

package-private members of the parent. You can use the inherited

members as is, replace them, hide them, or supplement them with new

members

它表示如果子类位于同一个包中,则子类可以访问父类的私有成员。我已经尝试过了,我无法访问子类中父类的私有成员

它说package-private不是private。

不,它表示

If the subclass is in the same package as its parent, it also inherits the package-private members of the parent

package-private(无修饰语)与private修饰语(private修饰语)不同。有关差异,请参阅https://docs.oracle.com/javase/tutorial/java/javaoo/accesscontrol.html。

package-private和private成员之间存在差异。除了类本身,任何人都不能访问私有成员。但是,软件包私有成员是那些仅属于该特定软件包的private成员,并且该特定软件包中的任何成员都可以访问这些成员。

java 子类访问父类私有成员_关于java:如果两个类都在同一个包中,子类是否可以访问父类私有成员..._第1张图片

你可能感兴趣的:(java,子类访问父类私有成员)