1. 程式人生 > >Java中static塊,構造塊,構造函數的執行順序

Java中static塊,構造塊,構造函數的執行順序

fat ring [] spa ide 構造 int 靜態塊 out

public class Father {
    static {
        System.out.println("Father靜態塊");
    }

    {
        System.out.println("Father構造塊");
    }

    public Father() {
        System.out.println("Father構造器");
    }

    void func1() {
        System.out.println("Father方法 1");
    }

    public static void main(String[] args) {
        Father father 
= new Son(); father.func1(); } } class Son extends Father { static{ System.out.println("Son靜態塊"); } { System.out.println("Son構造塊"); } public Son() { System.out.println("Son構造器"); } @Override void func1() { System.out.println(
"Son方法 1"); } void func2() { System.out.println("Son方法 2"); } }

結果:

Father靜態塊
Son靜態塊
Father構造塊
Father構造器
Son構造塊
Son構造器
Son方法 1

Java中static塊,構造塊,構造函數的執行順序