【发布时间】:2018-07-27 19:03:14
【问题描述】:
Angular>2中的组件继承非常简单,只需扩展父类即可:
import { ParentComponent } from './parent.component';
@Component({
selector: 'child',
templateUrl: './child.component.html'
})
export class ChildComponent extends ParentComponent {
}
这适用于很多用例。
现在,假设我有一个父类 Person,以及两个子类 Employee 和 Contractor。在我查询数据库之前,我不知道一个人是承包商还是雇员。
所以我的问题是,根据运行时收到的标志对 Person 进行子类化的最佳方法是什么?是否有适合此用例的设计模式?
【问题讨论】:
-
Typescript 编译下来,接口就会丢失。
标签: angular inheritance design-patterns polymorphism