我正在阅读 a Java Doc ,它说
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
它说子类可以访问父类的私有(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对于差异。
关于java - 如果两个类都在同一个包中,子类可以访问父类私有(private)成员吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889987/