Class类-super关键字

在编程中,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()时,可以向父类传递参数:

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关键字传递参数给父类构造函数:

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的构造函数。

  • 20
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向想享xh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值