類中包含7中成員:
一、靜態變量 static
二、final靜態常量 final static
三、靜態代碼塊 static{} //多個代碼塊順序執行
四、普通變量
五、普通代碼塊 {} //多個代碼塊順序執行
六、構造方法
七、普通方法
類中成員初始化方式(排出7):
非繼承類中:
首先執行1/2/3,執行順序爲順序執行(誰在前先執行誰)。
以後執行4/5,執行順序爲順序執行(誰在前先執行誰)。
而後執行6(默認執行無參構造方法)。
繼承類中:
首先執行父類1/2/3,執行順序爲順序執行(誰在前先執行誰),
隨即執行子類1/2/3,執行順序爲順序執行(誰在前先執行誰)。
以後執行父類4/5,執行順序爲順序執行(誰在前先執行誰),
而後執行父類6(若是子類沒有調用父類構造方法,則默認調用父類默認無參構造方法)。
隨即執行子類4/5,執行順序爲順序執行(誰在前先執行誰)。
而後執行子類6。
ide
代碼:this
* 每一個屬性設置斷點,啓動後能夠觀察執行順序。spa
父類:code
1 package com.lookcoder.haircutmember.vo; 2 3 public class TRoleVoAFather { 4 private String nameFather = "12"; 5 private Integer ageFather = 1; 6 private int ageiFather = 1; 7 { 8 System.out.println("father defualt method block2"); 9 } 10 private String defualtInitFather = getDefaultInit(); 11 { 12 System.out.println("father defualt method block1"); 13 } 14 private String getDefaultInit() { 15 return "father default init"; 16 } 17 // ---------------------------------------------------------------------------- 18 private static String aFather = "2"; 19 private final static String aaFather = getFinalStatic(); 20 static { 21 System.out.println("father default static method2"); 22 } 23 private static Integer bFather = 1; 24 static { 25 System.out.println("father default static method1"); 26 } 27 private final static Integer bbFather = 2; 28 private static String getFinalStatic() { 29 return "father final static aa"; 30 } 31 private final static Boolean bo = true; 32 // ---------------------------------------------------------------------------- 33 TRoleVoAFather() { 34 System.out.println("father default construct"); 35 } 36 TRoleVoAFather(String name, Integer age) { 37 this.nameFather = name; 38 this.ageFather = age; 39 } 40 // ---------------------------------------------------------------------------- 41 public void abc() { 42 System.out.println("abc method!"); 43 } 44 }
子類:blog
1 package com.lookcoder.haircutmember.vo; 2 3 public class TRoleVoA extends TRoleVoAFather{ 4 private int agei = 1; 5 { 6 System.out.println("defualt method block2"); 7 } 8 private Integer age = 1; 9 { 10 System.out.println("defualt method block1"); 11 } 12 private String name = "12"; 13 private String defualtInit = getDefaultInit(); 14 private String getDefaultInit() { 15 return "default init"; 16 } 17 // ---------------------------------------------------------------------------- 18 private final static Integer bb = 2; 19 private final static String aa = getFinalStatic(); 20 private static String getFinalStatic() { 21 return "final static aa"; 22 } 23 private final static Boolean bo = true; 24 private static String a = "2"; 25 private static Integer b = 1; 26 static { 27 System.out.println("default static method2"); 28 } 29 static { 30 System.out.println("default static method1"); 31 } 32 private static Integer b11= 1; 33 34 // ---------------------------------------------------------------------------- 35 TRoleVoA() { 36 System.out.println("default construct"); 37 } 38 TRoleVoA(String name, Integer age) { 39 super(name, age); 40 this.name = name; 41 this.age = age; 42 } 43 // ---------------------------------------------------------------------------- 44 public void showDefault() { 45 System.out.println("show default method!"); 46 } 47 }
啓動類:繼承
1 package com.lookcoder.haircutmember.vo; 2 3 public class TRoleVoARun { 4 public static void main(String[] args) { 5 TRoleVoA tRoleVo = new TRoleVoA("123", 123); 6 tRoleVo.showDefault(); 7 } 8 }