在Java中,super关键字在构造器中的使用有两种情况:
public class Parent {
public Parent() {
System.out.println("Parent constructor");
}
}
public class Child extends Parent {
public Child() {
super(); // 调用父类构造器
System.out.println("Child constructor");
}
}
public class Outer {
private int x;
public Outer(int x) {
this.x = x;
}
public class Inner {
private int y;
public Inner(int y) {
Outer.this.x = y; // 调用外部类的构造器
this.y = y;
}
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>