[英]JS: EcmaScript6 how to pass different number of parameters to extended class
我的課上有一個小問題。 這一定很容易,但是我無法找到解決方案。 Cuboid
類的效果很好,但是Cube
類並不是很好,我認為我以錯誤的方式使用了super
方法。
給我一點提示。 先感謝您。
class Cuboid { constructor(length, width, height) { this.length = length; this.width = width; this.height = height; } get surfaceArea() { return (this.length * this.width + this.length * this.height + this.height * this.width) * 2; } get volume() { return this.length * this.width * this.height; } } class Cube extends Cuboid { constructor(length) { super(length); this.height = length; } }
伙計們,您為什么反對我的問題? 真的不是很好
正如我建議的那樣,多維數據集是長方體,所有3維均相等。 因此,有2種選擇可以做到:
1。
class Cube extends Cuboid {
constructor(length) {
super(length, length, length);
}
}
2。
class Cuboid {
constructor(length, width, height) {
this.length = length || 0;
this.width = width || this.length;
this.height = height || this.width;
}
// ....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.