【java】關於java類的執行順序
阿新 • • 發佈:2018-12-11
先後順序如下!
1.靜態塊
2.塊
3.構造器
4.父類構造器
執行順序為:
1. 靜態塊
2. 父類構造器
3. 本類中的塊
4. 本類的構造器
父類:
package test922; public class Father { static { System.out.println("Father static方法"); } { System.out.println("Father 塊方法"); } public Father() { System.out.println("Father構造器"); } public void usualFatherFunction() { System.out.println("usualFatherFunction父類普通方法"); } }
本類:
/** * */ package test922; /** * @author rocling */ public class Oneself extends Father { static { System.out.println("Oneself static方法"); } { System.out.println("oneself 塊方法"); } /** * 本地構造器 */ public Oneself() { System.out.println("oneself構造器"); } public void usualOneselfFunction() { System.out.println(); } /** * @param args */ public static void main(String[] args) { Oneself oneself = new Oneself(); oneself.usualOneselfFunction(); oneself.usualFatherFunction(); // static只執行一次 } }
結果:
Father static方法 Oneself static方法 Father 塊方法 Father構造器 oneself 塊方法 oneself構造器
usualFatherFunction父類普通方法