代码语言:javascript复制
//初始化项目
npm init
//安装typescript
npm install typescript
//创建一个typescript文件 test.ts
//编译test.ts文件
tsc test.ts
代码语言:javascript复制//class代码范例
class Person{
protected name: String;
constructor(name:string){
this.name=name;
}
greet(){
return `${this.name}向你问好`;
}
}
class Student extends Person{
private major:string;
constructor(name:string,major:string){
super(name);
this.major=major;
}
studentGreet(){
return `${this.major}系的${this.name}向你问好`;
}
}
let xiaoming=new Student('小明','哲学');
console.log(xiaoming.studentGreet());