假设我有两节课,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)
我无法理解为什么这段代码的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).