我正在尝试扩展和重写 TypeScript 中单例类中的方法,这是单例类的代码:
class Singleton {
protected static _instance: Singleton;
protected constructor() { }
public static get instance() {
if (Singleton._instance === undefined) {
Singleton._instance = new Singleton();
}
return Singleton._instance;
}
public doWork() {
console.log('doing work in singleton...');
}
}
Run Code Online (Sandbox Code Playgroud)
扩展单例类:
class ExtendedSingleton extends Singleton {
protected static _instance: ExtendedSingleton;
protected constructor() {
super();
}
public static get instance() {
console.log('Creating Extended Singleton');
if (ExtendedSingleton._instance === undefined) {
ExtendedSingleton._instance = new ExtendedSingleton();
}
return ExtendedSingleton._instance;
}
public doWork() …Run Code Online (Sandbox Code Playgroud)