Java
1 // 演⽰⽗⼦类的变量⽅法的调⽤关系;
2
3 /**
4 * ⽗类
5 */
6 public class Father {
7 public int age = 20;
8
9 public Father(){
10 System.out.println(“⽗类的⽆参构造⽅法被调⽤了!”);
11 }
1212
13 public void method(){
14 System.out.println(“⽗类的method⽅法被调⽤了!”);
15 }
16 }
17
18 /**
19 * ⼦类
20 */
21 public class Son extends Father{
22 private int age = 30;
23
24 public Son(){
25 System.out.println(“⼦类的⽆参数的构造⽅法被调⽤了!”);
26 }
27
28 public void show(){
29 final int age = 40;
30 System.out.println(“age = “ + age);
31 System.out.println(“this.age = “ + this.age);
32 System.out.println(“super.age = “ + super.age);
33 }
34 }
35
36 // 测试
37 public class Test {
38 public static void main(_String[] args) {
39 Son son = new Son
();
40 son.show
();
41 son.method
();
42
}
43
}
44
45
// super⽬前理解为⽗类的对象就可以了; _

• 图解

对象在堆内存中,会单独开辟⼀块空间⽤来存储⽗内存的数据;
image.png

this和super的区别

image.png