//构造函数
class Person{
constructor(public name:string){}
}
//继承
class teacher extends Person{
constructor(public age:number){
super('jspang')
}
}
const teacher1 = new teacher(18);
console.log(teacher1.name);
console.log(teacher1.age);
本文介绍 TypeScript 中类的基本定义及如何实现继承。通过示例代码展示了构造函数的使用以及子类如何调用父类的构造函数。
//构造函数
class Person{
constructor(public name:string){}
}
//继承
class teacher extends Person{
constructor(public age:number){
super('jspang')
}
}
const teacher1 = new teacher(18);
console.log(teacher1.name);
console.log(teacher1.age);
1742
2102

被折叠的 条评论
为什么被折叠?