Mat*_*hel 7 abstract-class typescript
在以下示例中,我收到了 TypeScript 错误 Abstract property 'name' in class 'Minigame' cannot be accessed in the constructor.
我正在努力思考如何实现这一点。我无法将具体类的名称传递到super()调用中,因为在调用之前我无法访问对象的属性,并且我无法创建属性,static因为抽象类无法强制执行。
这应该如何组织以保证每个Minigame实例化一个Explanation对象,这需要具体类的 name 属性?使名称static(并删除抽象要求)真的是保持简单的最佳选择吗?
abstract class Minigame {
abstract name: string;
explanation: Explanation;
constructor() {
this.explanation = new Explanation(this.name);
}
}
class SomeGame extends Minigame {
name = "Some Game's Name";
constructor() {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
字符串有点难看,但你可以这样做:
abstract class Minigame {
abstract GetName(): string;
explanation: Explanation;
constructor() {
this.explanation = new Explanation(this.GetName());
}
}
class SomeGame extends Minigame {
GetName(): string {
return "Some Game's Name";
}
constructor() {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
这是基于 Tom Coton 的另一个解决方案,它不强制实现该getName方法。
只是将name抽象类作为构造函数参数移动。
class Explanation {
name: string;
constructor(name: string) {
this.name = name;
}
}
abstract class Minigame {
explanation: Explanation;
name: string;
constructor(name) {
this.name = name;
this.explanation = new Explanation(this.name);
}
}
class SomeGame extends Minigame {
constructor() {
super('some game')
}
}
const game = new SomeGame();
console.log({ game: game })
Run Code Online (Sandbox Code Playgroud)
您可以通过单击“运行”来检查
如果Explanation每次name设置时都需要重新创建,您还可以使用 setter 和 getter:
class Explanation {
name: string;
constructor(name: string) {
this.name = name;
}
}
abstract class Minigame {
explanation?: Explanation;
private _name?: string;
set name(name: string | undefined) {
this._name = name;
if(name) {
this.explanation = new Explanation(name);
}
}
get name(): string | undefined{
return this._name;
}
constructor(name?: string) {
this.name = name;
}
}
class SomeGame extends Minigame {
constructor() {
super('some game');
}
}
const game = new SomeGame();
console.log({ game: game })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3127 次 |
| 最近记录: |