super
super
super
关键字用于引用父类的成员(变量、方法或构造函数)。
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SuperExample.java
class Parent {
String name;
public Parent(String name) {
this.name = name;
}
public void printName() {
System.out.println("Parent name: " + name);
}
}
class Child extends Parent {
String childName;
public Child(String parentName, String childName) {
// 调用父类的构造函数
super(parentName);
this.childName = childName;
}
public void printAllNames() {
// 调用父类的方法
super.printName();
System.out.println("Child name: " + childName);
}
}
public class SuperExample {
public static void main(String[] args) {
Child child = new Child("John", "Tom");
child.printAllNames();
}
}
输出结果:
PLAINTEXT
1
2
Parent name: John
Child name: Tom
在上面的例子中,Child
类继承了 Parent
类,并使用 super
关键字调用了父类的构造函数和 printName
方法。