【发布时间】:2021-02-06 08:31:05
【问题描述】:
我创建了一个USer 类并创建了一个子类ADmin
class USer {
constructor(public name: string, public employees: string[]) { // wieso dann hier nochmal type zuordnen dachte das wäre das gleiche oder ?
}
login(this: User){
}
}
class ADmin extends USer {
constructor(name: string, employes: string[], public admin: string){
super(name, employes); // calls the constructor of the USer !
}
}
据我了解super()调用USer的构造函数,所以所有属性都继承到子类。
如果我不声明我得到的属性类型
Argument of type 'void' is not assignable to parameter of type 'string'
为什么我需要在ADmin 类中再次声明属性的类型?不应该把类型转给ADmin吗?
【问题讨论】:
-
您不需要,但在这种情况下,您需要参数,以便可以将其传递给父级...但是您可以拥有任何类型的构造函数参数。这样的定义可以是
constructor(public admin: string){ super("James", ["Sarah", "John"]);} -
对不起,我不明白你的解释,我编辑了我的问题以便澄清