前阵子看到阿里巴巴的一提面试题是关于java类的加载顺序

package com.mikey.demo.Test;

class FatherVariable{
    static {
        System.out.println("FatherVariable Static Constructor Code");
    }
    {
        System.out.println("FatherVariable Constructor Code");
    }
    public FatherVariable() {
        System.out.println("FatherVariable Constructor Method");
    }
}

class ChildVariable{
    static {
        System.out.println("ChildVariable Static Constructor Code");
    }
    {
        System.out.println("ChildVariable Constructor Code");
    }
    public ChildVariable() {
        System.out.println("ChildVariable Constructor Method");
    }
}

class Father{

    static FatherVariable fatherVariable = new FatherVariable();

    static {
        System.out.println("Father Static Constructor Code");
    }
    {
        System.out.println("Father Constructor Code");
    }
    public Father() {
        System.out.println("Father Constructor Method");
    }
}

class Child extends Father {

    static ChildVariable childVariable = new ChildVariable();

    static {
        System.out.println("Child Static Constructor Code");
    }
    {
        System.out.println("Child Constructor Code");
    }
    public Child() {
        System.out.println("Child Constructor Method");
    }
}

public class Clazz {
    public static void main(String[] args) {
        new Child();
        //父类静态变量
        //FatherVariable Static Constructor Code
        //FatherVariable Constructor Code
        //FatherVariable Constructor Method
        //父类静态代码块
        //Father Static Constructor Code
        //子类静态变量
        //ChildVariable Static Constructor Code
        //ChildVariable Constructor Code
        //ChildVariable Constructor Method
        //子类静态代码块
        //Child Static Constructor Code
        //父类构造代码块
        //Father Constructor Code
        //父类构造方法
        //Father Constructor Method
        //子类构造代码块
        //Child Constructor Code
        //子类构造方法
        //Child Constructor Method
    }
}
View Code

相关文章:

  • 2022-01-10
  • 2022-12-23
  • 2021-08-26
  • 2021-05-11
  • 2021-10-21
  • 2021-09-02
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2021-07-07
  • 2021-08-04
  • 2022-01-15
  • 2022-01-09
相关资源
相似解决方案