如果Son类继承Father类,Father类继承GrandFather类,那么new Son()创建对象的时候是否会执行GrandFather类里面的方法

答案:会,具体分析可以参考我这一篇博客https://www.cnblogs.com/skyvalley/p/13825859.html

代码如下

package com.example.jucdemo;

class GrandFather{
    public GrandFather(){
        System.out.println("aaaaaaaaaa");
    }
    {
        System.out.println("bbbbbbbbbbbbbbb");
    }
    static{
        System.out.println("cccccccccccc");
    }
}

class Father extends GrandFather{
    public Father(){
        System.out.println("1111111");
    }
    {
        System.out.println("2222222");
    }
    static{
        System.out.println("3333333");
    }
}
class Son extends Father{
    public Son(){
        //super();
        System.out.println("4444444");
    }
    {
        System.out.println("5555555");
    }
    static {
        System.out.println("6666666");
    }
}

public class MethodOrderTest {
    public static void main(String[] args) {
        new Son();
    }
}

输出结果如下

cccccccccccc
3333333
6666666
bbbbbbbbbbbbbbb
aaaaaaaaaa
2222222
1111111
5555555
4444444
原文地址:https://www.cnblogs.com/skyvalley/p/13825874.html