Typescript - 重载私有方法

时间:2015-06-22 06:43:55

标签: typescript private overloading

你好我想实现这个场景:

class A implements InterfaceForA
{
    a():void
    {
        this.b();
    }
}

Class B extends A
{
    private b():void
    {
        console.log('Hi');
    }
}

但它抛出:

  

错误TS2339:属性' b'在' A'。

类型中不存在

所以我更新了我的班级,现在它抛出了:

  

错误TS2415:Class' B'错误地扩展了基类' A'。类型具有私有财产的单独声明' b'。

使用代码:

class A implements InterfaceForA
{
    a():void
    {
        this.b();
    }

    private b():void
    {
        console.log('Hello');
    }
}

Class B extends A
{
    private b():void
    {
        console.log('Hi');
    }
}

在C ++中,我将A类方法b()设置为虚拟私有,问题就解决了。在JS中它根本不是问题。如何在TypeScript中执行此操作?

1 个答案:

答案 0 :(得分:8)

在C ++中,您实际上将其设置为受保护。私人会员是私人会员。

Typescript> = 1.3支持受保护的限定符。

请参阅:What is the equivalent of protected in TypeScript?