在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;
}
}
}