45
A
回答
48
更新
2014年11月12日。版本1.3的TypeScript可用幷包含受保護的關鍵字。
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;
}
}
老回答
打字稿只有private
- 不保護,這不僅意味着在編譯時檢查私人。
如果你想訪問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;
}
}
4
如何以下方法:
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;
}
}
由於num
變量被定義爲公共的,這將工作:
var b = new B(4);
b.num;
但因爲它是不是在接口中定義,這:
var b: MyType = new B(4);
b.num;
將導致The property 'num' does not exist on value of type 'MyType'
。
您可以在此playground中試用。
您也可以將其包裝在模塊中,而只導出接口,然後從其他導出的方法中返回實例(工廠),這樣變量的公共範圍將被「包含」在模塊中。
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'
修改版本在playground。
我知道這不是你要求的,但它非常接近。
1
相關問題
- 1. 什麼是React.PropTypes.node的TypeScript等效物?
- 2. PowerShell中'nohup'的等效物是什麼?
- 3. Dart中Platform.flush()的等效物是什麼?
- 4. 什麼是VB.NET中Program.cs的等效物
- 5. vb.net中nslookup的等效物是什麼?
- 6. jquery中的等效物是什麼
- 7. Windows的Cloud-Init等效物是什麼?
- 8. 什麼是blobstore「Create_upload_url」的GCS等效物?
- 9. 什麼是SIOCADDRT的IPv6等效物?
- 10. 什麼是MovieClip.CreateEmptyMovieClip的AS3等效物?
- 11. 什麼是PHP的$ _SERVER ['DOCUMENT_ROOT']在Java中的等效物
- 12. 什麼是在ADO.NET中使用Command.Prepare的VBA等效物
- 13. 在Linux中,NetBSD的FIONSPACE ioctl等效物是什麼?
- 14. JSON:什麼是在C#中的java地圖等效物
- 15. 在Windows 7 Process Explorer中,「VM Size」的等效物是什麼?
- 16. .hide()在佈局中保留位置的等價物是什麼?
- 17. 什麼是ASP MVC中的django中間件等效物?
- 18. 什麼是Windows 8中的wp7中心控制等效物?
- 19. jQuery中的YUI 3的Y.Array()的等效物是什麼
- 20. iOS:Objective-C中的事件偵聽器的等效物是什麼?
- 21. Windows批量中的bash的等效物是什麼?
- 22. WinRT應用程序中的app.config的等效物是什麼?
- 23. OpenLayers 3.0中的「fractionalZoom」的等效物是什麼?
- 24. pytorch中的theano.tensor.clip的等效物是什麼?
- 25. JavaScript中的mixpanel.get_property()的iOS等效物是什麼
- 26. Xamarin.Forms中的Android加載程序的等效物是什麼?
- 27. Flex緩動效果中的easeOutExpo等價物是什麼?
- 28. jQuery/JavaScript中這個cookie代碼的PHP等效物是什麼?
- 29. 什麼是C99中「解鎖」I/O功能的等效物?
- 30. OpenSSL.Connection中的解包等效物是什麼?
您可以投票了這個CodePlex上的問題提到:http://typescript.codeplex.com/workitem/125 – 2013-03-07 18:28:12
我已經更新了我的答案,以反映「protected」已登陸:https://github.com/Microsoft/TypeScript/pull/688 – Fenton 2014-09-26 07:01:46