ฉันต้องการประหยัดเวลาและใช้โค้ดทั่วไปซ้ำในชั้นเรียนซึ่งขยายคลาส PIXI (ไลบรารีตัวแสดงผล webGl 2d)
การเชื่อมต่อวัตถุ:
module Game.Core {
export interface IObject {}
export interface IManagedObject extends IObject{
getKeyInManager(key: string): string;
setKeyInManager(key: string): IObject;
}
}
ปัญหาของฉันคือรหัสภายในgetKeyInManagerและsetKeyInManagerจะไม่เปลี่ยนแปลงและฉันต้องการใช้ซ้ำไม่ให้ซ้ำกันนี่คือการนำไปใช้:
export class ObjectThatShouldAlsoBeExtended{
private _keyInManager: string;
public getKeyInManager(key: string): string{
return this._keyInManager;
}
public setKeyInManager(key: string): DisplayObject{
this._keyInManager = key;
return this;
}
}
สิ่งที่ฉันต้องการจะทำคือการเพิ่มโดยอัตโนมัติผ่านManager.add()ที่สำคัญที่ใช้ในการจัดการเพื่อการอ้างอิงวัตถุภายใน_keyInManagerวัตถุเองในทรัพย์สินของตน
ลองมาเป็นตัวอย่างกับ Texture นี่คือTextureManager
module Game.Managers {
export class TextureManager extends Game.Managers.Manager {
public createFromLocalImage(name: string, relativePath: string): Game.Core.Texture{
return this.add(name, Game.Core.Texture.fromImage("/" + relativePath)).get(name);
}
}
}
เมื่อผมทำthis.add()ผมต้องการวิธีการที่จะเรียกวิธีการซึ่งจะมีอยู่บนวัตถุที่ส่งกลับโดยGame.Managers.Manager add() Game.Core.Texture.fromImage("/" + relativePath)วัตถุนี้ในกรณีนี้จะเป็นTexture:
module Game.Core {
// I must extends PIXI.Texture, but I need to inject the methods in IManagedObject.
export class Texture extends PIXI.Texture {
}
}
ฉันรู้ว่านั่นIManagedObjectคืออินเทอร์เฟซและไม่สามารถมีการนำไปใช้งานได้ แต่ฉันไม่รู้ว่าจะเขียนอะไรเพื่อฉีดคลาสObjectThatShouldAlsoBeExtendedภายในTextureคลาสของฉัน รู้ว่ากระบวนการเดียวกันจะต้องสำหรับSprite, TilingSprite, Layerและอื่น ๆ
ฉันต้องการคำติชม / คำแนะนำ TypeScript ที่มีประสบการณ์ที่นี่ต้องเป็นไปได้ที่จะทำ แต่ไม่ใช่โดยการขยายหลายครั้งเนื่องจากสามารถทำได้เพียงครั้งเดียวในเวลานั้นฉันไม่พบวิธีแก้ปัญหาอื่นใด