[英]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.