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
相关产品推荐
相关产品推荐

