• java 带静态域的导出类创建时都发生了什么?


    先按从基类到导出类的顺序初始化静态域(之前已经初始化过的静态域不再初始化)

    再按从基类到导出类的顺序初始化类,即基类普通字段+基类构造器主体+导出类字段+导出类主体...

    package test;

    class PrintInfo{
    static String Info(String s){
    System.out.println(s);
    return s;
    }
    }
    class grandfather{
    static String grandfatherstatic=PrintInfo.Info("grandfatherstatic1");
    static String grandfastatic2=PrintInfo.Info("grandfatherstatic2");
    String fa=PrintInfo.Info("grandfatherString1");
    String fa2=PrintInfo.Info("grandfatherString2");
    grandfather(){
    PrintInfo.Info("grandfatherconstructor");
    }
    }
    class father extends grandfather{
    static String fatherstatic=PrintInfo.Info("fatherstatic1");
    static String fastatic2=PrintInfo.Info("fatherstatic2");
    String fa=PrintInfo.Info("fatherString1");
    String fa2=PrintInfo.Info("fatherString2");
    father(){
    PrintInfo.Info("fatherconstructor");
    }

    }
    class son extends father{
    static String sonstatic=PrintInfo.Info("sonstatic1");
    static String sonstatic2=PrintInfo.Info("sonstatic2");
    String son=PrintInfo.Info("sonString1");
    String son2=PrintInfo.Info("sonString2");
    son(){
    PrintInfo.Info("sonconstructor");
    }
    }
    public class Test {
    public static void main(String[] args) {
    new son();
    }
    }

    output:

    grandfatherstatic1
    grandfatherstatic2
    fatherstatic1
    fatherstatic2
    sonstatic1
    sonstatic2
    grandfatherString1
    grandfatherString2
    grandfatherconstructor
    fatherString1
    fatherString2
    fatherconstructor
    sonString1
    sonString2
    sonconstructor

  • 相关阅读:
    sp3485在rk3288上的应用
    wk2124 在 rk3288 上的适配与调试
    Android自定义View实现一个状态切换开关
    SQLite数据库入门
    Android如何动态申请应用权限?
    APK 如何实现应用热更新功能?
    Android 如何通过代码安装 APK?
    Android.mk 中如何拷贝任意文件
    Linux 下网络 IO 的多路复用
    Android hideSoftInputFromWindow 不能隐藏软键盘怎么办?
  • 原文地址:https://www.cnblogs.com/gjl-blog/p/8487691.html
Copyright © 2020-2023  润新知