java - 在子类中调用超级构造函数?
问题描述
如何在子类中调用超类构造函数来使这段代码工作?
class Ale {
protected int x = 1;
public Ale( int xx ) {
x = xx;
}
}
class Bud extends Ale {
private int y = 2;
public void display() {
System.out.println("x = " + x + " y = " + y);
}
}
解决方案
您可以像这样调用超级构造函数,
class Ale {
protected int x = 1;
public Ale(int xx) {
x = xx;
}
}
class Bud extends Ale {
Bud() {
super(75);
}
private int y = 2;
public void display() {
System.out.println("x = " + x + " y = " + y);
}
}
推荐阅读
- python - 使用 grid_mapping ValueError 保存 NetCDF
- python - 将列表中的每个元素与之前的所有连续元素进行比较
- python - 如何自动运行 SUMO 模拟 100 次(不允许用手)
- r - 列表对象中所有表的总行数并除以列?
- linux - SUID时如何通过base64二进制提升权限
- java - 为什么 Spring 不会自动运行正确的路径
- c - Telegram api在c中返回400错误请求
- excel - 将多个文件夹vba中的excel文件(同名)合并到一个电子表格中
- python - 尝试按相似属性对对象进行分组时,我得到重复的分组
- flutter - 分配给 IconButton (onPressed) 的方法在状态初始化时自动调用