2013-10-10 29 views
11

buTypescript'te temel sınıfın özelliklerine nasıl erişirsiniz?

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()); 

gibi kod kullanarak bir öneri vardı ve hatta 9 oy aldı. Ancak, resmi TS oyun alanına yapıştırdığınızda, http://www.typescriptlang.org/Playground/ size hata verir.

A'dan B'ye x mülküne nasıl erişilir?

cevap

27

kullanım this yerine 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

Şampiyonu! Üzgünüz, +1 –

+4

@AlexVaghin için yeterli saygınlık yok/cevap olarak işaretlemeli – basarat

İlgili konular