You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Raku语言中对应JavaScript和Python里super关键字的语法是什么?

JavaScript 类继承中的 super 关键字

在JavaScript(或Python)中实现类继承时,派生类必须使用super关键字来初始化自身属性,或是调用基类的构造函数与方法。以下是具体示例:

class Rectangle {
    constructor(length, width) {
        this.name = "矩形";
        this.length = length;
        this.width = width;
    }

    shoutArea() {
        console.log(
            `我是一个${this.name.toUpperCase()},我的面积是${this.length * this.width}`
        );
    }
    
    rectHello() {
        return "矩形语: 你好";
    }
}

class Square extends Rectangle {
    constructor(length) {
        // 调用基类构造函数,将边长同时作为长和宽传入
        super(length, length);
        this.name = "正方形";
    }
    
    squaHello() {
        // 调用基类的rectHello方法
        const h = super.rectHello();
        return "正方形语:" + h.split(':')[1];
    }
}

const rect = new Rectangle(6, 4);
rect.shoutArea(); // 输出:我是一个矩形,我的面积是24

const squa = new Square(5);
squa.shoutArea(); // 输出:我是一个正方形,我的面积是25

console.log(squa.squaHello()); // 输出:正方形语: 你好

内容的提问来源于stack exchange,提问作者uzluisf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 08:17:17