当前位置: 编程技术>java/j2ee
java父类和子类初始化顺序的深入理解
来源: 互联网 发布时间:2014-10-25
本文导语: 继承类的构建过程是向外扩散的,子类初始化了,父类一定也初始化了 代码如下:class Art{ Art(){ System.out.println("Art"); }}class Drawing extends Art{ Drawing(){ System.out.println("Drawing"); }}public class Cartoon extends Drawing { Cartoon(){ System.o...
继承类的构建过程是向外扩散的,子类初始化了,父类一定也初始化了
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