'… incorrectly extends base class static side' error when overriding static field in derived class

☆樱花仙子☆ 提交于 2019-12-05 10:08:13

You cannot redeclare a private field in a derived class. Use protected if you intend for derived classes to be able to redeclare or access the field.

This is enforced because static methods are also available in the derived class. For example, this code does something unexpected (if we ignore the compile error):

class Base {
    private static foo = 'base';

    static printName() {
        // Should always print 'base' because no one
        // else has access to change 'foo'
        console.log(this.foo);
    }
}

class Derived extends Base {
    private static foo = 'derived';
}
// Will actually print 'derived'
Derived.printName();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!