Angular Typescript中的对象继承
我试图在angular中使用typescript扩展一个类,但出现以下错误:Angular Typescript中的对象继承,angular,typescript,oop,Angular,Typescript,Oop,我试图在angular中使用typescript扩展一个类,但出现以下错误: export class Product { constructor(public id?: number, public name?: string, public brand?: string, public expiry?: Date) { } } export class StockItem extends Product { constructor(public id?: num
export class Product {
constructor(public id?: number, public name?: string,
public brand?: string, public expiry?: Date) {
}
}
export class StockItem extends Product {
constructor(public id?: number, public name?: string,
public brand?: string, public expiry?: Date, public category?: string,
public price?: number, public quantity?: number,
public sold?: number, public description?: string,
public status?: boolean, public expired?: boolean,
public addedOn?: Date)
super(id, name, brand, expiry);
}
错误行:
src/app/models/data.model.ts(96,6): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(96,28): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(97,6): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(97,31): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(98,6): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(99,6): error TS2391: Function implementation is missing or not immediately following the declaration.
您缺少构造函数体的
{}
,该构造函数体应包含对super的调用:
export class Product {
constructor(public id?: number, public name?: string,
public brand?: string, public expiry?: Date) {
}
}
export class StockItem extends Product {
constructor(id?: number, name?: string,
brand?: string, expiry?: Date, public category?: string,
public price?: number, public quantity?: number,
public sold?: number, public description?: string,
public status?: boolean, public expired?: boolean,
public addedOn?: Date) {
super(id, name, brand, expiry);
}
}
也不需要重新声明基类声明的字段(即,我从
id
、name
、brand
和expiration
中删除了public
修饰符,因为它们已经在基类型中声明) 括号不见了吗?关于stockitem构造函数