11-TypeScript中的名称空间

时间:2022-09-07 14:49:36

在后端开发语言中,比如C#中,可以将不同源代码文件中的代码通过名称空间组合到一起。一般一个类定义在一个源代码文件中,在功能上属于一个上下文的源代码文件通过名称空间进行组织。

在TypeScript中,可以将多个ts文件组织到一个名称空间中,这样调用方就可以使用名称空间和类名完成调用。在TypeScript中,通过module关键字定义名称空间,另外要通过名称空间完成成员

的访问,成员必须指定export关键字。

ts文件一:

module Hys{
export class Doctor{
public GetDoctorInfo(){
console.log("hello,doctor");
}
}
}

ts文件二:

module Hys{
export class BasicDoctor extends Doctor{
public GetBasicDoctorInfo(){
super.GetDoctorInfo();
}
}
}

调用方:

import hys=Hys;
var basicdocotr=new hys.BasicDoctor();
basicdocotr.GetBasicDoctorInfo();
basicdocotr.GetDoctorInfo();

欢迎加入QQ群讨论:573336726