typescript - 如何通过另一个类扩展类?
问题描述
我有树类:
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
and扩展ClassificatorOrganizationsModel
。
结果,我需要获得EduOrganizationModel
包括孩子在内的所有属性的课程。
所以,我不能这样做:
class EduOrganizationModel extends EduUnitModel, ClassificatorOrganizationsModel {
}
如何解决?
解决方案
您可以使用 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));
});
});
}
推荐阅读
- google-cloud-platform - 禁用手写文本检测
- javascript - (DiscordJS) 如何检测表情符号?
- javascript - React-redux 组件在状态更改后不会重新渲染
- flutter - Flutter 移除边框扩展磁贴
- node.js - 如何使多个数据库更新的 setTimeout 顺序?
- node.js - 使用本地图像编辑 Rich Embed
- android - Android Studio 设计器预览与设备上看到的不同
- sql - 按与 SQL Server 中的某些属性匹配的子表查询表
- python - 为什么我的 python 睡眠功能无法正常工作?
- python - python中MATLAB“结构单元”的最佳等价物是什么?