为什么我的子类没有在 java 中正确初始化变量

发布于 2024-11-10 03:20:48 字数 446 浏览 2 评论 0原文

在下面的代码中,myString 始终初始化为 null。我必须在 init() 或类似的函数中手动初始化。据我所知它与超类/子类有关,但我不明白确切的机制

public class A extends B {

    private String myString = "test";


    public static void main(String[] args) {
        new A();
    }

    public A() {
        super();

    }

    public void c() {
        System.out.println(myString);
    }

}

public class B {

    public B() {
        c();
    }

    public void c() {

    }
}

In the code below, myString is always initialized to null. I have to manually initialize in an init() or similar. As far as I can tell it is related to superclass/subclass but I don't understand the exact mechanism

public class A extends B {

    private String myString = "test";


    public static void main(String[] args) {
        new A();
    }

    public A() {
        super();

    }

    public void c() {
        System.out.println(myString);
    }

}

public class B {

    public B() {
        c();
    }

    public void c() {

    }
}

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

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

发布评论

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

评论(3

生生漫 2024-11-17 03:20:48

您的代码的问题是, myString 在类 A 的构造函数的开头初始化,但在超级构造函数之后(即类B)。由于您之前从类 B 的构造函数访问变量(通过调用重写方法 c 间接),因此您会得到此行为。

根据经验:如果您想避免意外行为,请不要在执行构造函数之前调用重写方法。

The issue with your code is, that myString is initialized at the begin of the constructor of class A but right after the super constructor (i.e. class B). Since you access the variable before from the constructor of class B (indirectly via call to overriden methode c) your get this behaviour.

As a rule of thumb: if you want to avoid unexpected behavior do not call overriden methods before the constructor has been executed.

七分※倦醒 2024-11-17 03:20:48

在完全创建对象并完成对超类构造函数的调用后,立即添加对 c(); 覆盖方法的调用。

将您的代码更改为此..

public class A extends B {

    private String myString = "test";


    public static void main(String[] args) {
        new A();
    }

    public A() {
        super();
         c();     // Include the call to c(); here ...

    }

    public void c() {
        System.out.println(myString);
    }

}

public class B {

    public B() {

    }

    public void c() {

    }
} 

 // Output : test

Add a call to c(); overidden method right after the object has been fully created and call to superclass constructor is done.

Change your code to this ..

public class A extends B {

    private String myString = "test";


    public static void main(String[] args) {
        new A();
    }

    public A() {
        super();
         c();     // Include the call to c(); here ...

    }

    public void c() {
        System.out.println(myString);
    }

}

public class B {

    public B() {

    }

    public void c() {

    }
} 

 // Output : test
心凉怎暖 2024-11-17 03:20:48

《Thinking in Java Second Edition》作者:Bruce Eckel,多态方法的行为
构造函数内部(第 337-339 页)。

Thinking in Java Second Edition by Bruce Eckel, Behavior of polymorphic methods
inside constructors (p. 337-339).

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