zoukankan      html  css  js  c++  java
  • 子父类 构造代码块 静态代码块 初始化过程

     1 class Fu{
     2     Fu(){//调用show方法  被子类重写  num=0;
     3         System.out.println("Fu constructor.....");
     4         show();//实际是子类方法//zi..show 0
     5     }
     6     static{
     7         
     8         System.out.println("fu静态代码块");
     9     }
    10     {
    11         System.out.println("FU构造代码块");
    12     }
    13     void show(){
    14         System.out.println("Fu show---");
    15     }
    16 }
    17 class Zi extends Fu{
    18     int num = 9;
    19     static{//Jvm加载时运行,在main之前
    20         int a=2;
    21         System.out.println("a="+a);
    22     }
    23     {//constrcuctor  9
    24         System.out.println("constrcuctor......"+num);
    25         num=10;//   改变num值 10
    26     }
    27     Zi(){//起始
    28         //super();默认是有super的  先父类的构造函数
    29                 //显示初始化   num=9,然后是构造代码块初始化
    30         System.out.println("Zi Constructor......"+num);//Zi  Constructor......  num=10
    31     }
    32     void show(){
    33         System.out.println("Zi show---"+num);
    34     }
    35 }
    运行结果:
    1 fu静态代码块
    2 a=2
    3 FU构造代码块
    4 Fu constructor.....
    5 Zi show---0
    6 constrcuctor......9
    7 Zi Constructor......10

    静态代码块会随着类的加载就加载,构造代码块在对象初始化之前运行

    1,运行父类中的静态代码块
    2,运行子类中的静态代码块
    3,通过new子类对象 开始初始化过程,首先不指定则调用super();
    4,super初始化前会先运行父类的构造代码块
    5,然后开始子类对应的父类构造函数初始化,若子类重写了父类方法 ,父类会被覆盖
    6,父类初始化完后,会运行子类中的构造代码块
    7,最后进行子类构造函数初始化。


  • 相关阅读:
    执行超过1个小时的SQL语句
    非周一回写销售预测
    openLDAP
    Windows下使用性能监视器监控SqlServer的常见指标
    ORA-01720: grant option does not exist for 'xxx.xxxx' (ORA-01720 ‘XXX’ 不存在授权选项)
    117 FP页面无法查看 此错误是JDK8.0.0.0版本的一个BUG,会导致工单重复回写,
    KPI
    Quatrz + Spring
    windows 脚本
    Spring集成Redis
  • 原文地址:https://www.cnblogs.com/bingzhikun/p/4719171.html
Copyright © 2011-2022 走看看