代码拉取完成,页面将自动刷新
// es6继承
class Point {
x;
y;
constructor(x, y) {
this.x = x;
this.y = y;
}
print() {
console.log(`Point:(${this.x},${this.y})`);
}
}
class ColorPoint extends Point {
color;
constructor(x, y, color) {
// 必须调用父类构造函数
super(x, y);
this.color = color;
}
// 覆盖父类方法
print() {
console.log(`ColorPoint:(${this.x},${this.y}) ${this.color}`);
}
}
const p = new Point(1, 2);
p.print(); // Point:(1,2)
const cp = new ColorPoint(3, 4, "red");
cp.print(); // ColorPoint:(3,4) red
console.log(cp instanceof Point); // ture
console.log(cp instanceof ColorPoint); // ture
console.log(Point.prototype.isPrototypeOf(cp)); // ture
console.log(ColorPoint.prototype.isPrototypeOf(cp)); // ture
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。