我有树类:
class ClassificatorOrganizationsModel {
protectedcode: string | undefined;
}
class EduUnitModel {
paren1tId: number | undefined;
paren2tId: number | undefined;
paren3tId: number | undefined;
phone: string | undefined;
}
export class EduOrganizationModel {
regionId: number | undefined;
addressId: number | undefined;
}
我需要将EduOrganizationModel
和EduUnitModel
扩展为类ClassificatorOrganizationsModel
。
结果,我需要获取包含所有属性(包括子级)的类EduOrganizationModel
。
所以,我不能这样做:
class EduOrganizationModel extends EduUnitModel, ClassificatorOrganizationsModel {
}
如何解决?
答案 0 :(得分:2)
您可以使用Mixins https://www.typescriptlang.org/docs/handbook/mixins.html 进行多重继承。
interface EduOrganizationModel extends EduUnitModel, ClassificatorOrganizationsModel {}
applyMixins(EduOrganizationModel, [EduUnitModel, ClassificatorOrganizationsModel]);
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
});
});
}