[英]How memory allocates to superclass and subclass members when we create object of subclass using superclass reference variable
class A {
int y = 10;
void m1() {
System.out.println("This is M1");
int b = 20;
}
}
public class B extends A {
int x = 10;
int y = 20;
void m1() {
System.out.println("This is M2");
}
public static void main(String[] args) {
A a = new B();
System.out.println(a.y);
a.m1();
}
}
What will be the memory allocation chart/diagram for this? 内存分配图表/图表将是什么?
new B
: new B
:
B()
: B()
构造函数:
A()
: A()
构造函数: A.this.y = 10;
A.this.y = 10;
B.this.x = 10; B.this.y = 20;
B.this.x = 10; B.this.y = 20;
B.this.x = 10; B.this.y = 20;
So the allocation is primarily done in the new
. 因此,分配主要在
new
完成。
The allocation done in the constructor and field initialisations can be illustrated in the next corner cast (to be avoided): 在构造函数和字段初始化中完成的分配可在下一个强制转换中说明(避免):
class A {
A() {
init(); // VERY BAD STYLE
}
protected void init() {
}
}
class B extends A {
String s1 = null;
String s2;
String s3 = "s3";
String s4;
String s5 = "s5";
B() {
// s1: null, s2: null, s3: null, s4: null, s5: null
// super() called, calling init()
// s1: "i1", s2: "i2", s3: "i3", s4: null, s5: null
// field initialisation:
// - s1 = null; s3 = "s3"; s5 = "s5";
// s1: null, s2: "i2", s3: "i3", s4: null, s5: "s5"
// remaining code of constructor
}
@Override
protected void init() {
// s1: null, s2: null, s3: null, s4: null, s5: null
s1 = "i1";
s2 = "i2";
s3 = "i3";
// s1: "i1", s2: "i2", s3: "i3", s4: null, s5: null
}
}
The above shows the starting life times of fields, and the unexpected moments if someone ever uses overridable methods in a constructor . 上面显示了字段的起始寿命,以及某人曾经在构造函数中使用可重写方法的意外时刻。
It also shows that in the constructor of A the super fields already exist (with default values null, 0, false, 0.0, ...
). 它还显示在A的构造函数中,超级字段已经存在(默认值为
null, 0, false, 0.0, ...
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.