The initialization sequence [JAVA] inherited when classes

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ALone_cat/article/details/94140936

We can write programs to view:

//父类
class Test{
    public Test(){
        System.out.println("父类实例变量");
    }
}
class StaticTest{
    public StaticTest(){
        System.out.println("父类静态变量");
    }
}
class People{
    private Test test = new Test();
    private static StaticTest staticTest = new StaticTest();
    static{
        System.out.println("父类静态块");
    }
    {
        System.out.println("父类实例块");
    }
    public People(){
        System.out.println("父类构造方法");
    }
}
//子类
class TestS{
    public TestS(){
        System.out.println("子类实例变量");
    }
}
class StaticTestS{
    public StaticTestS(){
        System.out.println("子类静态变量");
    }
}
class Student extends People{
    private TestS tests = new TestS();
    private static StaticTestS staticTests = new StaticTestS();
    static{
        System.out.println("子类静态块");
    }
    {
        System.out.println("子类实例块");
    }
    public Student(){
        System.out.println("子类构造方法");
    }
}
//主函数
public class TestDemo {
    public static void main(String[] args){
       Student student = new Student();
    }
}

operation result:

Static variable parent class
the parent class static block
subclass static variables
subclass static block
parent class instance variables
parent class instance blocks
parent class constructor
sub-class instance variables
subclass instance block
subclass constructors

 

 

Guess you like

Origin blog.csdn.net/ALone_cat/article/details/94140936