【发布时间】:2018-04-24 10:38:22
【问题描述】:
鉴于这个 TypeScript 类:
export class Person {
public constructor (public firstName: string, public lastName: string) {
}
public fullName(): string {
return `${this.firstName} ${this.lastName}`;
}
public WriteInfo() {
console.log(this.fullName());
}
}
在后代类中重写构造函数的正确方法是什么?
我有:
import { Person } from './class';
export class Employee extends Person {
public constructor (afirstName: string, alastName: string, public Salary: number) {
super(afirstName, alastName);
}
public WriteInfo() {
let s: string = `${this.fullName()} makes this much: -- $${this.Salary}`;
console.log(s);
}
}
但我不确定构造函数的声明是否正确,尤其是在默认属性方面。
Employee 的 firstName 和 lastName 参数也必须声明为 public 吗?我有什么工作,但我只想检查这是否正确/接受/适当/常规。
【问题讨论】:
-
我不是 TS 专家,但这对我来说看起来不错... fn 和 ln 已经在超类中声明为
public不需要这个,如果不好,TS 会直接打到你脸上 ;-) 这就是它存在的原因 :-) -
Thnaks @Legends
标签: typescript