TypeScript 中 protected 的等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15275730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:00:51  来源:igfitidea点击:

What is the equivalent of protected in TypeScript?

typescript

提问by Tahereh Farrokhi

What is the equivalent of protectedin TypeScript?

什么是相当于保护打字稿

I need to add some member variables in the base classto be used only in derived classes.

我需要在基类中添加一些仅在派生类中使用的成员变量。

回答by Fenton

Updates

更新

November 12th, 2014. Version 1.3 of TypeScript is available and includes the protected keyword.

2014 年 11 月 12 日。TypeScript 1.3 版可用,并包含受保护的关键字。

September 26th, 2014. The protectedkeyword has landed. It is currently pre-release. If you are using a very new version of TypeScript you can now use the protectedkeyword... the answer below is for older versions of TypeScript. Enjoy.

2014 年 9 月 26 日。protected关键字已落地。它目前是预发布。如果您使用的是新版本的 TypeScript,您现在可以使用protected关键字...下面的答案适用于旧版本的 TypeScript。享受。

View the release notes for the protected keyword

查看受保护关键字的发行说明

class A {
    protected x: string = 'a';
}

class B extends A {
    method() {
        return this.x;
    }
}

Old Answer

旧答案

TypeScript has only private- not protected and this only means private during compile-time checking.

TypeScript 只有private- 未受保护,这仅意味着在编译时检查期间是私有的。

If you want to access super.propertyit has to be public.

如果你想访问super.property它必须是公开的。

class A {
    // Setting this to private will cause class B to have a compile error
    public x: string = 'a';
}

class B extends A {
    method() {
        return super.x;
    }
}

回答by Nitzan Tomer

How about the following approach:

以下方法如何:

interface MyType {
    doit(): number;
}

class A implements MyType {
    public num: number;

    doit() {
        return this.num; 
    }
}

class B extends A {
    constructor(private times: number) {
        super();
    }

    doit() {
        return super.num * this.times; 
    }
}

Since the numvariable is defined as public, this will work:

由于num变量被定义为 public,这将起作用:

var b = new B(4);
b.num;

But since it's not defined in the interface, this:

但是由于它没有在接口中定义,所以:

var b: MyType = new B(4);
b.num;

will result in The property 'num' does not exist on value of type 'MyType'.
You can try it in this playground.

将导致The property 'num' does not exist on value of type 'MyType'.
你可以在这个游乐场试试。

You can also wrap it in a module while exporting only the interface, then from other exported methods you can return the instances (factory), that way the public scope of the variables will be "contained" in the module.

您也可以将它包装在一个模块中,同时只导出接口,然后从其他导出的方法中您可以返回实例(工厂),这样变量的公共范围将“包含”在模块中。

module MyModule {
    export interface MyType {
        doit(): number;
    }

    class A implements MyType {
        public num: number;

        doit() {
            return this.num; 
        }
    }

    class B extends A {
        constructor(private times: number) {
            super();
        }

        doit() {
            return super.num * this.times; 
        }
    }

    export function factory(value?: number): MyType {
        return value != null ? new B(value) : new A();
    }
}

var b: MyModule.MyType = MyModule.factory(4);
b.num; /// The property 'num' does not exist on value of type 'MyType'

Modified version in this playground.

这个游乐场的修改版本。

I know it's not exactly what you asked for, but it's pretty close.

我知道这不完全是你所要求的,但它非常接近。

回答by Nati Krisi

at least at the moment (version 0.9) protected are not mentioned in the specs

至少目前(版本 0.9)保护没有在规范中提到

http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf

http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf