gpt4 book ai didi

java - 私有(private)字段和方法可以继承吗?

转载 作者:行者123 更新时间:2023-12-01 19:57:32 25 4
gpt4 key购买 nike

我做了一些研究来了解私有(private)实例字段和方法是否由子类从其父类(super class)继承。

实际上,我在不同的论坛上看到了相互矛盾的答案,但最有说服力的答案是,就像 java 文档所说的那样,私有(private)字段和方法永远不会被继承,但是子类的实例为子类的私有(private)字段和方法分配了一些内存。父类(super class)。

但是,在我最可靠的文档来源,即“java in a nutshell 6th Edition”一书中,它说:

This existence of potentially inaccessible members seems to be in conflict with the statement that the members of a class are always accessible within the body of the class. To clear up this confusion, we define “inherited members” to mean those superclass members that are accessible.

Then the correct statement about member accessibility is: “All inherited members and all members defined in this class are accessible.” An alternative way of saying this is:

• A class inherits all instance fields and instance methods (but not constructors) of its superclass.

• The body of a class can always access all the fields and methods it declares itself. It can also access the accessible fields and members it inherits from its superclass.

因此,根据我的理解,我得出的结论是,子类从其父类(super class)继承了所有字段和方法(包括私有(private)字段和方法),但不知何故,子类的主体无法访问其私有(private)(以及最终其他不可见)成员父类(super class)。

如果我很好地理解了这本书所说的内容,这与java文档所说的内容(私有(private)成员甚至不能被继承)不矛盾吗?或者我在阅读这本书时错过了什么?

最佳答案

成员是否被继承主要与查找过程相关。如果例如a class B extends A,当语言规范说私有(private)成员不可继承时,这意味着 A 的私有(private)成员只属于 的实例B,同时将它们视为 A

这个的经典例子看起来像这样:

class B extends A {}

class A {
private void m() {}

public static void main(String[] args) {
B b = new B();
b.m(); // error: "cannot find symbol"
A a = b;
a.m(); // fine: m() is a member of A
}
}

确实有一个方法m(),我们可以在B的实例上调用它,但是方法查找过程无法找到它,除非我们正在查看B 作为 A

使用私有(private)字段,对象的内存中表示将包括其父类(super class)的私有(private)字段,即使我们说它们在 JLS 术语中不是继承的。

一些进一步的说明位于 §8.2 :

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.

以及 §8.3 :

A class inherits from its direct superclass and direct superinterfaces all the non-private fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class.

A private field of a superclass might be accessible to a subclass - for example, if both classes are members of the same class. Nevertheless, a private field is never inherited by a subclass.

( §8.4.8 也有类似的方法规则。)

这些澄清了继承确实与可访问性有关,但不存在严格的 1:1 对应关系。例如,在下面的例子中,字段x可以被class B访问,但不能被class B继承(根据上面的§8.3):

class Outer {
static class A {
private int x;
}
static class B extends A {
B() {
super.x = 1; // accessible, but must be qualified
}
}
}

一种说法是,可访问性是继承的必要条件,但不是充分条件。 (换句话说,继承需要可访问性,但反之则不然。)

通俗地说,私有(private)成员是继承的可能是正确的,因为 1) 子类对象存储其父类(super class)的私有(private)变量,2) 可以在子类实例上调用父类(super class)的私有(private)方法。然而,JLS 并不是这样使用这个词的。

关于java - 私有(private)字段和方法可以继承吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49076656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com