我与打字稿工作,我有带班打字稿,静态方法继承
之间的静态继承问题谁能给我解释一下下面的结果:
class Foo {
protected static bar: string[] = [];
public static addBar(bar: string) {
this.bar.push(bar);
}
public static logBar() {
console.log(this.bar);
}
}
class Son extends Foo {
protected static bar: string[] = [];
}
class Daughter extends Foo {}
Foo.addBar('Hello');
Son.addBar('World');
Daughter.addBar('Both ?');
Foo.logBar();
Son.logBar();
Daughter.logBar();
当前的结果:
[ 'Hello', 'Both ?' ]
[ 'World' ]
[ 'Hello', 'Both ?' ]
,但我想:
[ 'Hello' ]
[ 'World' ]
[ 'Both ?' ]
我有没有重新声明静态bar
属性的解决方案?
谢谢!
删除*静态* –