我的类(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 维相等的长方体。因此有两种选择:
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;
}
// ....
关于javascript - JS : EcmaScript6 how to pass different number of parameters to extended class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538199/