zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    利用Vista新技术WCF开发构建服务系统
    WCF开发入门的六个步骤
    C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(一)让物体动起来①
    silverlight
    初探C#3.0
    通过托管代码和 Windows Vista 智能卡 API 来保护您的数据
    用例图
    类图
    Win7下MSN显示两个窗口问题
    设计模式初探
  • 原文地址:https://www.cnblogs.com/gjl-blog/p/8487691.html
Copyright © 2011-2022 走看看