如何使用TypeScript从类中使用静态方法记录信息



这是一个学校项目。我在使用方法从类中获取要记录的名称时遇到问题。我有一个类可以制造怪物,另一个类则可以扩展它。

abstract class genMonster {
constructor(
public id: string,
public name: string,
public weaknesses: string[],
public location: string,
public challenge: number,
public mortality: boolean = false,
public safety: number,
) {}
monsterLogger() {
return this
}
}
class ghost extends genMonster {
constructor(id: string, name: string, weaknesses: string[], location: string, challenge: number, mortality: boolean, safety: number, public type: string, public signs: string[]) {
super(id, name, weaknesses, location, challenge, mortality, safety);
}
get info() {
return console.log(this.name, this.type, this.signs);
}
}

我有怪物物体

const Jerry = new ghost("0234", "Jerry", ["soap", "attractive people"], "-74.4835, 171.4803",  3, false, 2, "boring", ["libra", "ectoplasm puddles", "yamaha piano music"]);
const Patricia = new ghost("8765", "Patricia", ["being sent to the corner", "milk and cookies"], "-89.3921, -30.4079",  1, false, 2, "spooky", ["stray drawing supplies", "messy living rooms", "spilled milk"]);
const Lola = new ghost("4569", "Lola", ["makeup remover", "tied shoelaces"], "-61.6134, -90.1008",  10, false, 10, "boss music", ["water tasting like cotton candy", "spontaneous laughing", "socks mysteriously becoming fun and colorful"]);

这是我尝试过的方法,但不起作用

class organize extends genMonster{
constructor(id: string, name: string, weaknesses: string[], location: string, challenge: number, mortality: boolean, safety: number){
super(id, name, weaknesses, location, challenge, mortality, safety)
}
public static getNames(){
return this.name
}
}
console.log(organize.getNames());
//logs 'organize'

感谢提供的任何帮助

organizegetNames是静态的,并返回构造函数名称本身("organize"(。是否要获取重影实例本身的名称(JerryPatriciaLola(?如果是…

console.log(Jerry.name, Patricia.name, Lola.name);

由于静态方法无法访问实例字段,因此在创建每个怪物时,您可能需要在静态字段(takenNames(上记录怪物的所有名称。

然后可以有一个返回takenNames的静态方法(getTakenNames(

abstract class genMonster {
static takenNames: string[] = [];
static getTakenNames(): string[] {
return ghost.takenNames;
}
constructor(
public id: string,
public name: string,
public weaknesses: string[],
public location: string,
public challenge: number,
public mortality: boolean = false,
public safety: number,
) {}
monsterLogger() {
return this
}
}
class ghost extends genMonster {

constructor(id: string, name: string, weaknesses: string[], location: string, challenge: number, mortality: boolean, safety: number, public type: string, public signs: string[]) {
super(id, name, weaknesses, location, challenge, mortality, safety);
ghost.takenNames.push(name);
}
get info() {
return console.log(this.name, this.type, this.signs);
}
}
const Jerry = new ghost("0234", "Jerry", ["soap", "attractive people"], "-74.4835, 171.4803",  3, false, 2, "boring", ["libra", "ectoplasm puddles", "yamaha piano music"]);
const Patricia = new ghost("8765", "Patricia", ["being sent to the corner", "milk and cookies"], "-89.3921, -30.4079",  1, false, 2, "spooky", ["stray drawing supplies", "messy living rooms", "spilled milk"]);
const Lola = new ghost("4569", "Lola", ["makeup remover", "tied shoelaces"], "-61.6134, -90.1008",  10, false, 10, "boss music", ["water tasting like cotton candy", "spontaneous laughing", "socks mysteriously becoming fun and colorful"]);
console.log(genMonster.getTakenNames())

操场

相关内容

  • 没有找到相关文章

最新更新