构造函数调用中的意外输出

发布于 2024-12-09 12:08:28 字数 1462 浏览 0 评论 0原文

class SuperclassA {
    protected int superValue;                              // (1)
    SuperclassA() {                                        // (2)
        System.out.println("Constructor in SuperclassA");
        this.doValue();                                    // (3)
    }
    void doValue() {                                       // (4)
        this.superValue = 911;
        System.out.println("superValue: " + this.superValue);
    }
}

class SubclassB extends SuperclassA {
    private int value = 800;                               // (5)
    SubclassB() {                                          // (6)
        System.out.println("Constructor in SubclassB");
        this.doValue();
        System.out.println("superValue: " + this.superValue);
    }
    void doValue() {                                       // (7)
        System.out.println("value: " + this.value);
    }
}

public class Javaapp {
    public static void main(String[] args) {
        System.out.println("Creating an object of SubclassB.");
        new SubclassB();                                   // (8)
    }
}

为什么我的输出是:

Creating an object of SubclassB.
Constructor in SuperclassA
value: 0
Constructor in SubclassB
value: 800
superValue: 0

我想这应该是这样的:

Creating an object of SubclassB.        
Constructor in SuperclassA
value: 800
Constructor in SubclassB
value: 800
superValue: 0
class SuperclassA {
    protected int superValue;                              // (1)
    SuperclassA() {                                        // (2)
        System.out.println("Constructor in SuperclassA");
        this.doValue();                                    // (3)
    }
    void doValue() {                                       // (4)
        this.superValue = 911;
        System.out.println("superValue: " + this.superValue);
    }
}

class SubclassB extends SuperclassA {
    private int value = 800;                               // (5)
    SubclassB() {                                          // (6)
        System.out.println("Constructor in SubclassB");
        this.doValue();
        System.out.println("superValue: " + this.superValue);
    }
    void doValue() {                                       // (7)
        System.out.println("value: " + this.value);
    }
}

public class Javaapp {
    public static void main(String[] args) {
        System.out.println("Creating an object of SubclassB.");
        new SubclassB();                                   // (8)
    }
}

why is my output:

Creating an object of SubclassB.
Constructor in SuperclassA
value: 0
Constructor in SubclassB
value: 800
superValue: 0

i suppose this should be like:

Creating an object of SubclassB.        
Constructor in SuperclassA
value: 800
Constructor in SubclassB
value: 800
superValue: 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2024-12-16 12:08:28

您在第一个 value 输出中看到 0 的原因是,value 的字段初始值设定项(将其设置为 800)将不会运行,直到就在 SubclassB 的构造函数即将被调用之前。在构造 SuperclassA 期间(必须在构造子类之前发生),value 在其未初始化状态下被观察到,即 0。

The reason you see 0 for the first value output is that the field initializer for value (which sets it to 800) will not be run until just before the constructor for SubclassB is about to be called. During the construction of SuperclassA (which must occur prior to the construction of the subclass,) value is observed in its unitialized state, which is 0.

卸妝后依然美 2024-12-16 12:08:28

SuperclassAthis.doValue() 正在调用 SubClassBvalue 中的 doValue() 方法在创建 SubClassB 之前, 不会被初始化。有关详细信息,请参阅 http://en.wikipedia.org/wiki/Method_overriding

SuperclassA's this.doValue() is calling the doValue() method in SubClassB and value does not get initialized until SubClassB is being created. See http://en.wikipedia.org/wiki/Method_overriding for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文