专注JavaWeb开发 首页制式『默认式 OR 时间轴 网站地图|RSS订阅|设为首页|加入收藏
当前位置:首页 >Java >Java基础 > 正文

Java类的加载顺序(子,父类实验)

作者: 紫寒1120 发布时间: 2016-05-25 浏览次数:

父类:

public class Parents {

private Integer i = 9;

private int p = print(" Parents.p initialized");

protected Integer j;

{

System.out.println("语句块" + "i = " + i + ", j = " + j);

}


Parents() {

System.out.println("i = " + i + ", j = " + j);

j = 39;

}


private static int x1 = print("static Parents.x1 initialized");


static int print(String s) {

System.out.println(s);

return 47;

}


}


子类:

public class Child extends Parents  {

private int k = print("Child.k initialized");

 public Child() {

   System.out.println("k = " + k);

   System.out.println("j = " + j);

 }

 private static void dd()

 {

System.out.println("dd ");

 }

 private static  int x2 =

   print("static Child.x2 initialized");

 public static void main(String[] args) {

   System.out.println("Child constructor");

    new Child();

   

 }

}


运行结果:

static Parents.x1 initialized

static Child.x2 initialized

Child constructor

 Parents.p initialized

语句块i = 9, j = null

i = 9, j = null

Child.k initialized

k = 47

j = 39

执行顺序,大致如下:

先初始化父类静态变量--->子类静态变量---->父类非静态成员变量(语句块,按顺序加载)---->父类构造器--->子类非静态成员变量--->子类构造器

QQ群: 武汉地区(142173356)验证消息JavaWeb1024

网友共有【】评论,点击查看



x 微信关注我们 随时获取最新网站资讯