java父类和子类初始化顺序的深入理解
程序员文章站
2023-12-18 12:52:58
继承类的构建过程是向外扩散的,子类初始化了,父类一定也初始化了复制代码 代码如下:class art{ art(){ system.out....
继承类的构建过程是向外扩散的,子类初始化了,父类一定也初始化了
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