如何初始化具有方法的类

时间:2019-03-28 08:20:29

标签: javascript asp.net

这是我的父母班

export class Book
{
    constructor(source: price){
        this.name= source.name;
        this.auther= source.auther;
        this.series= source.series;
        this.pricedeatils= source.pricedeatils && source.pricedeatils.map(a=>new price(a));
    }
    name: String;
    auther: String;
    series: String;
    pricedeatils: Array<price>;
}

这是子类,具有方法setPrice

export class price
{

    constructor(source: price){
        this.price1 = source.price1;
        this.price2 = source.price2;
        this.price3 = source.price3;
        this.price4 = source.price4;
        this.price5 = source.price5;
        this.price6 = source.price6;
        this.price7 = source.price7;
    }
    price1: Number; 
    price2: Number;
    price3: Number;
    price4: Number;
    price5: Number;
    price6: Number;
    price7: Number; 
    public setPrice( lcol:number,lnprice:number)
    {     
        this["price"+ lcol]=  lnprice:  
    }
}

我像这样初始化我的课程

setInis_master(){

    let details1    :price[]= []; 

    let ls_dynstructure:  Book= {
        name: "";
        auther: "";
        series: "";
        pricedeatils:  :details1     

    }
    return ls_dynstructure;
}



iniprice(){
    let ls_price:  price= {
        price1: 0; 
        price2: 0;
        price3: 0;
        price4: 0;
        price5: 0;
        price6: 0;
        price7: 0;
    }
    return ls_price:
}

this.lbook=  new Book(this.setInis_master())

let obvm1:price =  new price(this.iniprice())
obvm1 =await this.iniObject(irow,obvm1)   
this.lbook.pricedeatils.push(obvm1)

但是我无法在子类中调用方法

this.lbook.pricedeatils[0].setPrice(1,70)

可能是什么原因?我是错误地初始化了课程吗?

1 个答案:

答案 0 :(得分:2)

子类应扩展其父类以调用其方法 fObj.add('<span id="bla">Hello</span>')