java - 如果我们在父类构造函数中添加一个 super(),这个 super() 在创建子类的对象时会调用哪个类?
问题描述
在下面的示例中,super() 将在创建 Square 对象时在 Shape 构造函数中调用哪个类?
package com.company;
class Shape {
public Shape() {
super();
System.out.println("inside Shape class default constructor");
}
}
class Rectangle extends Shape {
public Rectangle() {
super();
System.out.println("inside Rectangle class default constructor");
}
}
class Square extends Rectangle {
public Square() {
super();
System.out.println("inside Square class default constructor");
}
}
public class Ex2 {
public static void main(String[] args) {
Square sq = new Square();
}
}
解决方案
它将调用java.lang.Object
类的构造函数,它是 Java 中所有类的默认父级。
推荐阅读
- google-api - Google Drive API v3 & Google Picker:更改文件的下载权限
- swift - 如何自定义 MKAnnotation pin 的图像
- azure - 在 Azure DW(突触)中使用 Max 进行数据透视
- python - 调用函数来检查输入是否能被 7 整除
- json - 如何使用 Flutter 将 Json 数据中的所有图像显示到 Carousal 中
- arrays - 如何根据相同的键将一个对象放入另一个对象中?
- python-3.x - 使用 Voila 运行 ipynb 文件时,Chrome 浏览器中不会显示任何交互式小部件
- swift - 带有重复的本地通知
- javascript - angular 9在单个元素滚动上添加类
- c++ - C++ 初学者,尝试打印值时访问冲突