小编kam*_*o94的帖子

如何防止特定方法的继承?

假设我有两节课,A并且B. B扩展A并因此继承其所有方法。如果我愿意的话,我也可以覆盖它们。我的问题是我是否可以阻止B继承A. 到目前为止我所尝试的看起来像这样。

// setup
class A {
  constructor(x) {
    this.x = x;
  }

  valueOf() {
    return this.x;
  }

  toString() {
    return `{x:${this.x}}`;
  }
}

class B extends A {
  constructor(x) {
    super(x);
    delete this.valueOf;
  }
}

delete B.prototype.valueOf;

// example
const a = new A(42);
const b = new B(42);

// should work
console.log(a.valueOf());
// should throw TypeError, not a function
console.log(b.valueOf());
Run Code Online (Sandbox Code Playgroud)

javascript inheritance ecmascript-6

7
推荐指数
2
解决办法
3781
查看次数

大O表示法 - 请解释O(log2n)

我无法理解为什么这段代码的O(log 2 ^ n)为其Big O表示法:

for (int i = n; i>=1; i=i/2){
    sum = i+j;
}
Run Code Online (Sandbox Code Playgroud)

我以为它会是O(n).

java big-o for-loop

2
推荐指数
1
解决办法
2960
查看次数

标签 统计

big-o ×1

ecmascript-6 ×1

for-loop ×1

inheritance ×1

java ×1

javascript ×1