您的位置:首页 > 编程语言 > Java开发

JAVA类的初始化执行顺序探讨

2015-05-08 09:57 260 查看
package hello;

public class Test {
{
System.out.println("Parent无参构造方法");
}

static {
System.out.println("Parent静态方法");
}

public static void main(String args[]) {
Child c1 = new Child("哈哈哈1");
System.out.println("***********");
Child c2 = new Child();
}
}

class Parent {
public Parent() {
System.out.println("Parent无参构造方法");
}

public Parent(String s) {
System.out.println("Parent无参构造方法  " + s);
}

{
System.out.println("Parent无参构造方法");
}

static {
System.out.println("Parent静态方法");
}
}

class Child extends Parent {
public Child() {
System.out.println("Child无参构造方法");
}

public Child(String s) {
super("调用Parent带参数方法");
System.out.println("Child无参构造方法  " + s);
}

{
System.out.println("Child无参构造方法");
}

static {
System.out.println("Child静态方法");
}
}


先不运行自己看下结果,然后再运行下。后续我会详解运行过程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: