Java-类初始化顺序-2

来源:互联网 发布:剑灵可爱召唤捏脸数据 编辑:程序博客网 时间:2024/05/16 17:53
package cn.yjx;public class Test3 {public static void main(String[] args) {new B();}}class A {private P p1 = new P("A -- p1");static P p3 = new P("A -- p3");public A() {System.out.println("A()");}public P p2 = new P("A -- p2");static {new P("A -- static");}{new P("A{...}");}}class B extends A {private P p1 = new P("B -- p1");static P p3 = new P("B -- p3");public B() {System.out.println("B()");}public P p2 = new P("B -- p2");static {new P("B -- static");}{new P("B{...}");}}class P {public P(String s) {System.out.println(s);}}

运行结果:


总结:

父类
静态
成员变量
{}代码块
构造方法
子类


0 0