java 继承的执行顺序

来源:互联网 发布:贵州广电网络网上缴费 编辑:程序博客网 时间:2024/05/17 23:18

直接看代码:

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.demo.test;

public class B {
    
    private String symbol;
    
    public B() {
        System.out.println("super B...");
    }
    public B(String symbol) {
        this.symbol = symbol;
        System.out.println("super B ..."this.symbol);
    }

    static {
        System.out.println("registry Driver.....");
    }

    public void play() {
        System.out.println("play basketball....");
    }

    public String calculate() {
        return "Hello B";
    }

}
package com.example.demo.test;

public class C extends B {

    public C() {
        System.out.println("son C....");
    }

    public C(String symbol) {
        super(symbol);
        System.out.println("son C...." + symbol);
    }

    public static void main(String[] args) {
        new B();
        new C();
        new C("ok");

        // registry Driver..... 1.new B();调用父类的静态构造代码块
        // super B...           2.new B(); 执行B类的空参构造器
        // super B...           3.new C(); 执行隐式的super()
        // son C....            4.new C(); 执行自己的空参构造器
        // super B ...ok        5.new C("ok");执行显示调用的super(smybol)//如果没有显示的调动super("ok") 是不会执行父类的带参数的构造器的 !!!!
        // son C....ok          6.new C("ok");执行自己的带参数构造函数
    }

}