java父类和子类初始化顺序的深入理解
作者:
本篇文章是对java父类和子类初始化顺序进行了详细的分析介绍,需要的朋友参考下
继承类的构建过程是向外扩散的,子类初始化了,父类一定也初始化了
class Art{
Art(){
System.out.println("Art");
}
}
class Drawing extends Art{
Drawing(){
System.out.println("Drawing");
}
}
public class Cartoon extends Drawing {
Cartoon(){
System.out.println("Cartoon");
}
public static void main(String[] args) {
Art cartoon = new Cartoon();
}
}
输出为:
Art
Drawing
Cartoon
复制代码 代码如下:
class Art{
Art(){
System.out.println("Art");
}
}
class Drawing extends Art{
Drawing(){
System.out.println("Drawing");
}
}
public class Cartoon extends Drawing {
Cartoon(){
System.out.println("Cartoon");
}
public static void main(String[] args) {
Art cartoon = new Cartoon();
}
}
输出为:
复制代码 代码如下:
Art
Drawing
Cartoon