2016-12-22 5 views
1

私は抽象クラスSectionを持っています。これは、有効であるか無効である可能性のある文書のセクションを表すために使用されます。このようなセクションには、埋め込みセクションもあります。無効な内部セクションが含まれているセクションは有効ではありません。TypeScriptの特定の型から派生した型のプロパティのプロパティ値を取得しますか?

私は、検証プロセスがperformValidation()によって呼び出さされているMySectionの内側のセクションとしてそれらを使用する目的のためのクラスASection1ASection2を作成しました。

MySectionクラスから派生した型のプロパティを取得する方法を教えてください。私は以下のように、抽象クラスリフレクションロジックの助けが必要です。

abstract class Section { 

    constructor(public name: string) { 
    } 

    performValidation(): boolean { 

     let result: boolean = true; 

     //HELP IS NEEDED HERE!!! 
     //get all properties of this instance 
     //whose type inherit from "Section" 
     //and execute Validate() on each of them 
     //one at a time, if any of them returns 
     //false, return false. 

     return this.Validate(); 
    } 

    abstract Validate(): boolean; 
} 

class ASection1 extends Section { 
    constructor() { 
     super("Section example 1"); 
    } 
    Validate(): boolean { 
     //validation logic goes here 
    } 
} 

class ASection2 extends Section { 
    constructor() { 
     super("Section example 2"); 
    } 
    Validate(): boolean { 
     //validation logic goes here 
    } 
} 

class MySection extends Section { 

    constructor() { 
     super("My Section"); 
    } 

    subsection1: ASection1; 
    subsection2: ASection2; 

    prop1: number; 

    Validate(): boolean { 
     return this.prop1 > 100; 
    } 

} 

//run 

let mySect = new MySection(); 
mySect.prop1 = 101; 
mySect.subsection1 = new ASection1(); 
mySect.subsection2 = new ASection2(); 
mySect.performValidation(); 

ありがとうございます。

+1

不明な個々のプロパティではなく、マップの配列にセクションを保存するだけの理由はありますか? –

答えて

0

同じタイプと同じ意味の2つ以上のプロパティがある場合は、毎回の代わりに配列を使用してください。各サブセクションにサブセクション配列を追加します。検証では、すべてのサブセクションをチェックし、いずれかのサブセクションの検証失敗ループがfalseを返した場合、サブセクションの検証に失敗した場合は検証を続行します。

abstract class Section { 
    protected subsections: Array<Section> = []; 

    constructor(public name: string) { 
    } 

    performValidation(): boolean { 

     let result: boolean = true; 

     for (var i = 0; i < this.subsections.length; i++) { 
      if (!this.subsections[i].Validate()) { 
       return; 
      } 
     } 

     return this.Validate(); 
    } 

    public addSubsection(section: Section) { 
     this.subsections.push(section); 
    } 

    abstract Validate(): boolean; 
} 

class ASection1 extends Section { 
    constructor() { 
     super("Section example 1"); 
    } 

    Validate(): boolean { 
     //validation logic goes here 
    } 
} 

class ASection2 extends Section { 
    constructor() { 
     super("Section example 2"); 
    } 
    Validate(): boolean { 
     //validation logic goes here 
    } 
} 

class MySection extends Section { 

    constructor() { 
     super("My Section"); 
    } 

    prop1: number; 

    Validate(): boolean { 
     return this.prop1 > 100; 
    } 

} 

//run 

let mySect = new MySection(); 
mySect.prop1 = 101; 
mySect.addSubsection(new ASection1()); 
mySect.addSubsection(new ASection2()); 
mySect.performValidation(); 
関連する問題