typescript - 如何在打字稿中正确实现和扩展?
问题描述
当我编写 OsProject 然后学生类从它扩展时,我必须删除我在学生构造函数中的初始化并将其替换为 super() 并且我必须将初始化程序添加到我在学生类中的属性中。我认为我的代码有问题,因为在这种情况下,构造函数中的 3 个参数没有用,我无法理解它,而且在这种情况下我也无法理解 super 的意义何在?有必要吗?完全当我必须使用超级?
interface Person{
firstName: string;
lastName : string;
age: number;
}
/////////////////////////////////////////////
abstract class Project{
projectName: string = "sds";
budget: number = 1;
calculateBudget(){
return this.budget*2
}
abstract changeName(name: string): void
}
class OsProject extends Project{
changeName(name: string){
this.projectName = name
}
}
//////////////////////////////////////
class Student extends OsProject implements Person{
firstName: string = "x";
lastName: string = "y";
age: number = 1;
constructor(firstName: string, lastName: string, age: number){
super()
//this.firstName = firstName;
//this.lastName = lastName;
//this.age = age;
}
print(){
console.log(this.firstName)
}
}
const myStudent:Student = new Student("x","y",22)
myStudent.print()
解决方案
推荐阅读
- python - 所有列的线图
- console - DataLayer(Chrome 开发工具)不返回数据
- javascript - 函数总是返回相同的值
- google-apps-script - 如何以编程方式将脚本文件从一个 google 工作表复制到另一个工作表?
- python - cefpython 和 POST 错误 401(未经授权)
- c - 在 [ C ] 中查找字符串函数
- r - 为什么地图功能在蒙特卡洛不起作用
- karate - 空手道:表单数据参数未记录在 POST 请求中
- javascript - 如何在画布javascript中随机打印一次绘图?
- javascript - 如何从颤动的graphQL发送多行字符串?