简体   繁体   English

JS:EcmaScript6如何将不同数量的参数传递给扩展类

[英]JS: EcmaScript6 how to pass different number of parameters to extended class

I'm having a little problem with my class. 我的课上有一个小问题。 It got to be something very easy, but I just cant find out the solution. 这一定很容易,但是我无法找到解决方案。 Class Cuboid works well, but the class Cube is just not okey, I think I've used the super method in a wrong way. Cuboid类的效果很好,但是Cube类并不是很好,我认为我以错误的方式使用了super方法。

Just give me a little hint. 给我一点提示。 Thank you in advance. 先感谢您。

 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; } } 

Guys, why do you downvote my question? 伙计们,您为什么反对我的问题? It's not really nice... 真的不是很好

As I suggesting Cube is Cuboid with all 3 dimension equal. 正如我建议的那样,多维数据集是长方体,所有3维均相等。 So there is 2 options to do it: 因此,有2种选择可以做到:

1. 1。

class Cube extends Cuboid {
  constructor(length) {
    super(length, length, length);
  }
}

2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM