类中包含7中成员:
1、静态变量 static
2、final静态常量 final static
3、静态代码块 static{} //多个代码块顺序执行
4、普通变量
5、普通代码块 {} //多个代码块顺序执行
6、构造方法
7、普通方法
类中成员初始化方式(排出7):
非继承类中:
首先执行1/2/3,执行顺序为顺序执行(谁在前先执行谁)。
之后执行4/5,执行顺序为顺序执行(谁在前先执行谁)。
然后执行6(默认执行无参构造方法)。
继承类中:
首先执行父类1/2/3,执行顺序为顺序执行(谁在前先执行谁),
随即执行子类1/2/3,执行顺序为顺序执行(谁在前先执行谁)。
之后执行父类4/5,执行顺序为顺序执行(谁在前先执行谁),
然后执行父类6(如果子类没有调用父类构造方法,则默认调用父类默认无参构造方法)。
随即执行子类4/5,执行顺序为顺序执行(谁在前先执行谁)。
然后执行子类6。
代码:
* 每个属性设置断点,启动后可以观察执行顺序。
父类:
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 }
子类:
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 }