Java-为什么它打印 null?
public class Base {
public Base() {
x = 0;
bar();
}
public Base(int x) {
this.x = x;
foo();
}
public void foo() {
System.out.println("Base.foo : " + x);
}
private void bar() {
System.out.println("Base.bar:" + x.toString());
}
protected Integer x;
}
public class Derived extends Base {
public Derived() {
bar();
}
public Derived(int x, int y) {
super(x);
this.y = y;
}
public void foo() {
System.out.println("Derived.foo : " + x + ", " + y);
}
public void bar() {
System.out.println("Derived.bar:" + x.toString() + ", " + y.toString());
}
private Integer y;
public static void main(String[] args) {
Base b = new Derived(10, 20);
}
}
为什么它打印 "Derived.foo:" 10, nulll
而不是 20 而不是 null? y 是 Derived 的私有变量,它被初始化为 20。它在它的范围内。那么为什么它是 null 呢?
public class Base {
public Base() {
x = 0;
bar();
}
public Base(int x) {
this.x = x;
foo();
}
public void foo() {
System.out.println("Base.foo : " + x);
}
private void bar() {
System.out.println("Base.bar:" + x.toString());
}
protected Integer x;
}
public class Derived extends Base {
public Derived() {
bar();
}
public Derived(int x, int y) {
super(x);
this.y = y;
}
public void foo() {
System.out.println("Derived.foo : " + x + ", " + y);
}
public void bar() {
System.out.println("Derived.bar:" + x.toString() + ", " + y.toString());
}
private Integer y;
public static void main(String[] args) {
Base b = new Derived(10, 20);
}
}
Why does it print "Derived.foo:" 10, nulll
and not 20 instead of the null?
y is a private variable of Derived, and it was initialized with 20. it's in its scope.. so why is it null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
因为超级构造函数首先被调用(
super(x)
)。这个超级构造函数调用方法 foo。然后 Derived 构造函数将 y 初始化为 20 (this.y = y
)。因此,当调用 foo 时,y
尚未初始化。在构造函数中调用可重写方法是一种不好的做法,因为正如您刚刚注意到的,它可以在部分构造的对象上调用重写方法。
Because the super constructor is first called (
super(x)
). This super constructor calls the method foo. Then the Derived constructor initializes y to 20 (this.y = y
). So when foo is called,y
is not initialized yet.It's a bad practice to call overridable methods in constructors, because, as you just noticed, it can call overridden methods on partially constructed objects.
println
来自方法foo
,该方法是从Base
的构造函数调用的,而该构造函数是从Derived
的构造函数调用的构造函数(通过super
)在初始化y
之前。所以空值是预期的。The
println
comes from methodfoo
which is called fromBase
's constructor, which is called fromDerived
's constructor (viasuper
) before you initializey
. So the null value is expected.这样做是因为超类 (
Base
) 构造函数在设置y
成员变量之前调用Derived.foo()
。所以这是基本的操作顺序:
然后之后
It does that because the superclass (
Base
) constructor callsDerived.foo()
before they
member variable has been set.So here is the basic order of operations:
Then after that
如果您跟踪构造函数的调用,就会更清楚:
Derived(int x, int y)
构造函数Base(int x)
foo()
方法println()
y
变量As您可以看到,在您尝试打印出该值后,y 变量就被设置了。这就是为什么您会看到
null
的未初始化值。从构造函数调用可重写的方法是一个常见的错误,因为它可能允许未初始化的变量在对象完全构造之前转义。
If you follow the calls from the constructor it makes it clearer:
Derived(int x, int y)
constructorBase(int x)
x
variablefoo()
methodprintln()
y
variableAs you can see, the y variable is getting set after you try to print out the value. That is why you are seeing the uninitialized value of
null
.Calling overridable methods from a constructor is a common mistake, as it can allow uninitialized variables to escape before the object is fully constructed.
当您进入超类的构造函数时,
y
尚未实例化。因此,对foo()
的调用将有一个null
y
。When you get into the constructor of the super class
y
is not yet instantiated yet. So the call tofoo()
will have anull
y
.它由超级构造函数调用触发的
Derived.foo()
打印出来,然后用 20 进行初始化。所以在打印时它仍然为空。It is printed by
Derived.foo()
triggered by Super-Constructor call before it is initialized with 20 afterwards. So while printing it is still null.所有答案确实都是正确的,但下次您可以在打印某些内容的函数中放置一个断点。
然后,您可以简单地检查调用堆栈并通过阅读正在调用的部分来跟踪代码。
这样您就可以发现 y 不会被初始化。
祝你好运!
罗尔
All the answers are indeed correct but next time you can maybe put a breakpoint in the function where you are printing something.
Then you can simply examine the call stack and folow the code by reading the parts that are being called.
This way you could have traced that y would not be initialized.
Good luck!
Roel
因为在基类构造函数中调用 foo() 时 y 尚未初始化。
链条消失了
主要->导出(x,y) ->基数(x) ->初始化 x, foo()。但是,因为您在 Derived 内部启动调用,该调用会覆盖 foo(),所以衍生的 foo() 实际上是由解释器执行的。
because y has not been initialised when foo() is called in the Base class constructor.
the chain goes
main -> Derived(x,y) -> Base(x) -> initialise x, foo(). However, because you're starting the call inside Derived, which overrides foo(), derived's foo() is actually executed by the interpreter.