码迷,mamicode.com
首页 > 其他好文 > 详细

子类继承父类,方法的执行顺序

时间:2019-06-17 17:38:16      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:stat   ext   执行   代码   类构造   构造   str   color   string   

public class HelloA {


    static { System.out.println("static A"); }

    { System.out.println("I‘m A class"); }

    public HelloA() {
        System.out.println("HelloA");
    }

}

 class HelloB extends HelloA {
    static { System.out.println("static B"); }

    { System.out.println("I‘m B class"); }

    public HelloB() {
        System.out.println("HelloB");
    }

    public static void main(String[] args) {
        new HelloB();
    }
}

//打印结果:

  static A
  static B
  I‘m A class
  HelloA
  I‘m B class
  HelloB

 

总结:执行顺序为:

  父类静态代码块

  子类静态代码块

  父类普通方法

  父类构造方法

  子类普通方法

  子类构造方法

子类继承父类,方法的执行顺序

标签:stat   ext   执行   代码   类构造   构造   str   color   string   

原文地址:https://www.cnblogs.com/shuaiding/p/11040630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!