首页 > 解决方案 > 如何在打字稿中正确实现和扩展?

问题描述

当我编写 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()

标签: typescriptoop

解决方案


推荐阅读