ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
当基类和继承类都被标记为`@Service()`装饰器时,**属性**就支持继承性。在创建时,继承有装饰属性的类将收到这些属性上的初始化类实例。 ``` import 'reflect-metadata'; import { Container, Token, Inject, Service } from 'typedi'; @Service() class InjectedClass { name: string = 'InjectedClass'; } @Service() class BaseClass { name: string = 'BaseClass'; @Inject() injectedClass: InjectedClass; } @Service() class ExtendedClass extends BaseClass { name: string = 'ExtendedClass'; } const instance = Container.get(ExtendedClass); // instance的`name`属性带有 "ExtendedClass "值(覆盖了基类) // `injectedClass`属性为`InjectedClass`类的实例 console.log(instance.injectedClass.name); // 输出"InjectedClass" console.log(instance.name); // 输出 "ExtendedClass" ```