java中初始化顺序

来源:互联网 发布:韩国网络剧奇迹在线 编辑:程序博客网 时间:2024/05/17 23:04
链接:https://www.nowcoder.com/questionTerminal/4d458835a8684eb6a7afef3096637578
来源:牛客网

初始化过程是这样的: 
1.首先,初始化父类中的静态成员变量和静态代码块,按照在程序中出现的顺序初始化; 
2.然后,初始化子类中的静态成员变量和静态代码块,按照在程序中出现的顺序初始化; 
3.其次,初始化父类的普通成员变量和代码块,在执行父类的构造方法;
4.最后,初始化子类的普通成员变量和代码块,在执行子类的构造方法;


链接:https://www.nowcoder.com/questionTerminal/4d458835a8684eb6a7afef3096637578
来源:牛客网

What will be printed when you execute the following 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
class C {
    C() {
        System.out.print("C");
    }
}
 
class A {
    C c =new C();
 
    A() {
        this("A");
        System.out.print("A");
    }
 
    A(String s) {
        System.out.print(s);
    }
}
 
class Test extends A {
    Test() {
        super("B");
        System.out.print("B");
    }
 
    publicstatic void main(String[] args) {
        new Test();
    }
}

  • BB
  • CBB
  • BAB
  • None of the above
选 B
原创粉丝点击