2013-10-10 8 views
11

このTypescriptでどのように基本クラスのプロパティにアクセスしますか?

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

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

var b:B = new B(); 
alert(b.method()); 

のようなコードを使用しての提案があったと言っても9票を得ました。しかし、それを公式のTS遊び場に貼るとき http://www.typescriptlang.org/Playground/ それはあなたとエラーを与える。

BのAのxプロパティにアクセスするには?

答えて

27

使用thisではなくsuper

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

class B extends A { 
    // constructor(){super();} 
    method():string { 
     return this.x; 
    } 
} 

var b:B = new B(); 
alert(b.method()); 
+2

チャンピオン!申し訳ありませんが、私は十分な評判を持っていません。 –

+4

@AlexVaghin uは回答できる – basarat

関連する問題