Skip to content

在编程中,super关键字通常用于引用父类(超类)的属性或方法。以下是在不同语言中super关键字的使用示例: JavaScript (ES6 Class Syntax)

Javascript
1class Parent {
2    display() {
3        console.log("Parent class");
4    }
5}
6
7class Child extends Parent {
8    display() {
9        super.display(); // 调用父类的display方法
10        console.log("Child class");
11    }
12}
13
14let child = new Child();
15child.display();

此处super.display()同样用于调用父类Parent的display方法。

super关键字在访问或修改状态(如成员变量)时,也可以发挥作用。例如,在Java和JavaScript中:

Java
1class Parent {
2    protected String name = "Parent";
3
4    public void showName() {
5        System.out.println("Name in Parent: " + name);
6    }
7}
8
9class Child extends Parent {
10    private String name = "Child";
11
12    public void showAllNames() {
13        System.out.println("Name in Child: " + this.name); // Accessing own state
14        System.out.println("Name in Parent (using super): " + super.name); // Accessing parent's state
15    }
16}
17
18public class Main {
19    public static void main(String[] args) {
20        Child child = new Child();
21        child.showAllNames();
22    }
23}

在这个例子中,super.name用于访问父类Parent的状态(成员变量)。 JavaScript

Javascript
1class Parent {
2    constructor() {
3        this.name = "Parent";
4    }
5
6    showName() {
7        console.log("Name in Parent: " + this.name);
8    }
9}
10
11class Child extends Parent {
12    constructor() {
13        super();
14        this.name = "Child";
15    }
16
17    showAllNames() {
18        console.log("Name in Child: " + this.name);
19        console.log("Name in Parent (accessing via super): " + super.name);
20    }
21}
22
23let child = new Child();
24child.showAllNames();

在这段JavaScript代码中,super.name同样用于访问父类Parent的状态(成员变量)。请注意,在构造函数中通过super()调用父类的构造函数来初始化父类的状态。

可以向super传参吗

确实可以在某些编程语言中向super传递参数,尤其是当涉及类的构造函数以及继承链中的方法重写时。以下是两种支持这种功能的语言示例:

Python 在Python中,当你在子类的构造函数(即__init__方法)中使用super()时,可以向父类传递参数:

python
class Parent:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def say_hello(self):
        print(f"Hello, I am {self.name}.")

class Child(Parent):
    def __init__(self, name, age, grade):
        super().__init__(name, age)  # 向父类传递参数
        self.grade = grade

    def introduce(self):
        super().say_hello()  # 调用父类方法
        print(f"I am in grade {self.grade}.")

child = Child("Alice", 10, 5)
child.introduce()

在这个例子中,super().__init__(name, age)将子类Child的构造函数中接收到的参数传递给了父类Parent的构造函数。

JavaScript (ES6 Classes) 在JavaScript中,你也可以在子类构造函数中通过super关键字传递参数给父类构造函数:

javascript
class Parent {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    sayHello() {
        console.log(`Hello, I am ${this.name}.`);
    }
}

class Child extends Parent {
    constructor(name, age, grade) {
        super(name, age);  // 向父类传递参数
        this.grade = grade;
    }

    introduce() {
        super.sayHello();  // 调用父类方法
        console.log(`I am in grade ${this.grade}.`);
    }
}

let child = new Child("Alice", 10, 5);
child.introduce();

在JavaScript的例子中,super(name, age)将子类Child构造函数接收的参数传递给了父类Parent的构造函数。

Released under the MIT License.