Java中的类继承之后的各种调用顺序
import static java.lang.System.*;
public class SwitchTest
{
public static void main(String[] args)
{
new Leaf();
new Leaf();
}
}
class Root
{
static {
System.out.println("Root static init block!");
}
{
System.out.println("Root normal init block!");
}
public Root()
{
System.out.println("Root non-par construct function!");
}
}
class Mid extends Root
{
static{
System.out.println("Mid static init block!");
}
{
System.out.println("Mid normal init block!");
}
public Mid()
{
System.out.println("Mid non-param construct function!");
}
public Mid(String msg)
{
this();
System.out.println("Mid param construct function! msg:" + msg);
}
}
class Leaf extends Mid
{
static {
out.println("Leaf static init block");
}
{
out.println("Leaf normal init block");
}
public Leaf()
{
super(" Call parent construct in Leaf! ");
out.println("Leaf construct function.");
}
}
This will output:
Root static init block!
Mid static init block!
Leaf static init block
Root normal init block!
Root non-par construct function!
Mid normal init block!
Mid non-param construct function!
Mid param construct function! msg: Call parent construct in Leaf!
Leaf normal init block
Leaf construct function.
Root normal init block!
Root non-par construct function!
Mid normal init block!
Mid non-param construct function!
Mid param construct function! msg: Call parent construct in Leaf!
Leaf normal init block
Leaf construct function.