之前面试或做题的时候总是会遇到问一个子类继承一个父类后,里面各自有静态代码块,构造器,方法等的执行顺序的问题,总是感觉好像是什么什么样,现在自己来写个例子就知道了:
父类:
public class FatherDemo { public static int f1 = 3; private static String name = "father"; public FatherDemo(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father类==无惨构造器============="); } public FatherDemo(String name){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father类==带参构造器============="); this.name = name; System.out.println("===f1:"+f1+"=====name:"+name+"========"); } static{ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father类==static代码块============="); } { System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father类==代码块============="); } public static void mothod0(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father类==static方法mothod0============="); } public void method(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father类==method方法============="); } }
子类:
public class ChildDemo extends FatherDemo{ public static int f2 = 3; private static String name = "child"; public ChildDemo(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo类==无惨构造器============="); } public ChildDemo(String name){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo类==带参构造器============="); this.name = name; System.out.println("===f1:"+f1+"=====name:"+name+"========"); } static{ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo类==static代码块============="); } { System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo类==代码块============="); } public static void mothod0(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo类==static方法mothod0============="); } public void method(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("===4444=====我是ChildDemo类==method方法============="); } }
测试类:
public static void main(String[] args) { ChildDemo child = new ChildDemo(); child.method(); System.out.println(); ChildDemo demo = new ChildDemo("demo"); demo.method();}
执行结果: ===f1:3=====name:father======== ========我是Father类==static代码块============= ===f1:3=====name:child======== ========我是ChildDemo类==static代码块============= ===f1:3=====name:father======== ========我是Father类==代码块============= ===f1:3=====name:father======== ========我是Father类==无惨构造器============= ===f1:3=====name:child======== ========我是ChildDemo类==代码块============= ===f1:3=====name:child======== ========我是ChildDemo类==无惨构造器============= ===f1:3=====name:child======== ===4444=====我是ChildDemo类==method方法=============
===f1:3=====name:father======== ========我是Father类==代码块============= ===f1:3=====name:father======== ========我是Father类==无惨构造器============= ===f1:3=====name:child======== ========我是ChildDemo类==代码块============= ===f1:3=====name:demo======== ========我是ChildDemo类==带参构造器============= ===f1:3=====name:demo======== ===f1:3=====name:demo======== ===4444=====我是ChildDemo类==method方法=============
由此可以看出,执行的顺序为:
父类静态属性 --》父类静态代码块--》子类静态属性--》子类静态代码块--》父类代码块--》父类无参构造--》子类代码块--》子类无参构造--》子类方法
如上发现父类和子类的静态方法都没有执行,有意思的是当我实例化子类的带参构造的时候,父类的子类的静态代码块就不执行了,而且执行的顺序变为了: 父类代码块--》父类无参构造器--》子类代码块--》子类带参构造器--》子类method方法