-1

我正在尝试打印namefromextended类。但得到一个错误:Property 'name' does not exist on type 'Employee'.

class Person {
    #name:string;
    getName(){
        return this.#name;
    }
    constructor(name:string){
        this.#name = name;
    }
}

class Employee extends Person {

    #salary:number;
    constructor(name:string, salary:number = 0){
        super(name);
        this.#salary = salary;
    }
    async giveRaise(raise:number):Promise<void> {
        this.#salary += raise;
        await this.#storySalary;
    }

    pay():void {
        console.log(`¤ ${this.#salary} is paid to ${this.name}.`);
    }
    async #storySalary():Promise<void> {
        console.log(`Salary ¤ ${this.#salary} is stored for ${this.name}`);
    }
}

const NewEmployee = new Employee('Sha', 300);
console.log(NewEmployee.getName(), NewEmployee.pay())

现场演示

4

1 回答 1

1

#name:string;meanname是私有属性,不能扩展私有变量。

您可能正在寻找的是protected name: string

class Person {
    protected name:string;
    getName(){
        return this.name;
    }
    constructor(name:string){
        this.name = name;
    }
}
于 2021-11-01T04:51:35.077 回答