错误:尝试在继承中使用超类结构(TypeScript 类)时发生问题。

huangapple go评论52阅读模式
英文:

Error with super structure in inheritance attempt (typescript classes)

问题

class Character {
    public readonly name: string;
    public readonly level: number;

    constructor(name: string, level: number) {
        this.name = name;
        this.level = level;
    }

    walk() {
        return `${this.name} walking!`;
    }

    showMyLevel() {
        return `${this.name} has the level ${this.level}`;
    }
}

class Wizard extends Character {
    private readonly staff: string;

    constructor(name: string, level: number, staff: string) {
        super(name, level);
        this.staff = staff;
    }

    fireBall() {
        return `${this.name} fire ball!`;
    }
}

const wizard1 = new Wizard('Ray', 8, 'Cajado');
console.log(wizard1.fireBall());
英文:

class Character {
    public readonly name: string;
    public readonly level: number;

    constructor(name: string, level: number) {
        this.name = name;
        this.level = level;
    }

    walk() {
        return `${this.name} walking!`
    }

    showMyLevel() {
        return `${this.name} has the level ${this.level}`
    }
}

class Wizzard extends Character {
    private readonly cajado: string;

    constructor(cajado: string) {
        super(name, level);
        this.cajado = cajado;
    }

    fireBall() {
        return `${this.name} fire ball!`
    }

}

const wizzard1 = new Wizzard('Ray', 8);

console.log(wizzard1.fireBall());

I would like my Wizard class to inherit my Character class and implement a constructor with one more item. However in super() I am getting errors

const name: void
@deprecated

'name' is deprecated.ts(6385)
lib.dom.d.ts(17877, 5): The declaration was marked as deprecated here.
Argument of type 'void' is not assignable to parameter of type 'string'.ts(2345)
Cannot find name 'level'. Did you mean the instance member 'this.level'?ts(2663)

I thought this could be happening because the variables are private in readonly, but changing it to public returns the same problem. Would you help me?

答案1

得分: 1

你需要在 Wizzard 类中添加与 Character 类匹配的参数。

class Wizzard extends Character {
    private readonly cajado: string;

    constructor(name: string, level: number, cajado: string) {
        super(name, level);
        this.cajado = cajado;
    }

    fireBall() {
        return `${this.name} fire ball!`
    }
}

另外,请注意正确的拼写是 Wizard,而不是 Wizzard

英文:

You need to add parameters to the Wizzard class that match the Character class.

class Wizzard extends Character {
    private readonly cajado: string;

    constructor(name: string, level: number, cajado: string) {
        super(name, level);
        this.cajado = cajado;
    }

    fireBall() {
        return `${this.name} fire ball!`
    }

}

Also note that Wizard is the correct spelling, not Wizzard.

huangapple
  • 本文由 发表于 2023年2月9日 02:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定