对象创建的过程

流程结果

对象构建之前

  1. 父类 static property 声明 赋值
  2. 父类 static{}
  3. 子类 static property 声明 赋值
  4. 子类 static{}

对象开始创建

  1. 进入子类 constructor() 默认spuer() 调用进入父 类 constructor()
  2. 父类 property 开始声明 赋值
  3. 运行 not static{}
  4. ——————————————返回 子类 constructor()
  5. 子类 property 声明 赋值
  6. 运行 not static{}

物料

public class Test08 {
    public static void main(String[] args) {
        Zi zi = new Zi();
    }
}
class Fu{
    private static int i = getNum("(1)i"); // 3
    private int j = getNum("(2)j");
    static{    // 1
        print("(3)父类静态代码块");
    }
    {
        print("(4)父类非静态代码块,又称为构造代码块");
    }
    Fu(){
        print("(5)父类构造器");
    }
    public static void print(String str){
        System.out.println(str + "->" + i);
    }
    public static int getNum(String str){
        print(str);
        return ++i;
    }
}
class Zi extends Fu{
    private static int k = getNum("(6)k"); //
    private int h = getNum("(7)h");
    static{  // 2
        print("(8)子类静态代码块");
    }
    {
        print("(9)子类非静态代码块,又称为构造代码块");
    }
    Zi(){
        print("(10)子类构造器");
    }
    public static void print(String str){
        System.out.println(str + "->" + k);
    }
    public static int getNum(String str){
        print(str);
        return ++k;
    }
}

贴上运行结果
在这里插入图片描述

来debug 一步步分析详解

在这里插入图片描述这里对象是从new 开始

new 开始之前

虽然感觉实际编写中没人这样去写代码,但还是了解了一下 一下阶段比较无用
此时对象还没有被构建

  1. 运行 父 类的 static 变量 声明
  2. —— static 变量 赋值调用的 static 方法 运行到static 方法 //完成赋值
  3. 运行 父 类的 static{} 代码块
  4. ————————————————
  5. 开始 运行 子类 static 变量 声明
  6. —— 可以直接赋值 使用 static 完成赋值
  7. 运行 子类 static{} 代码块

到目前为止 都开没开始运行 进行对象的创建,
说实话,我也不知道以后在开发中,谁会这样去写类,这些顺序有啥妙用

开始 调用 构造器了

debug 了半天。终于又回到了开始的地方
在这里插入图片描述

  1. 进入了子类构造器 运行第一行代码 这里有隐藏的 super()

  2. 进入 父 类的构造器。 完成父类 构造器 运行

  3. 此时 不会返回子类构造, 开始 成员变量赋值

  4. 运行 父 类 非static{}

  5. ——————————————

  6. 开始运行 构造器的{}
    到此 父类的全部内容都算完成,接下来就是子类完成一样的过程

  7. 回到 子类 。给子类 成员变量 赋值

  8. 运行 非static{} 代码块

  9. 运行 构造器{}

到这里整个类终于在完成了创建 全是细节,但又感觉没啥用= =

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值