簡體   English   中英

如何不在此 class 中重復相同的構造函數?

[英]how to not repeat the same constructor in this class?

我有一個程序有很多類似的課程。

問題是G0G1G2G3類具有相同的constructor()結構(但這有一些小的差異)

// this code is repeated on every child
this.x = _xyzObj.x ?? GcodeAPI.previusX ?? 0;
this.y = _xyzObj.y ?? GcodeAPI.previusY ?? 0;
this.z = _xyzObj.z ?? GcodeAPI.previusZ ?? 0;

但問題出在哪里?

通常用於調用 class 我使用類似這樣的東西

new G1({x:1, y:2, z: 10}).getCode();

正如您在示例中看到的,
G1 class 將數據的 object 作為參數。

因為這對於其他子類也一樣
G0G1G2G3 ,等等...


我有興趣看看是否有辦法
將重復的constructor()代碼放入父 class ( Gcommands )

✅ 就像我對方法所做的那樣( getCode();

...是的,我試過了,但是...
由於 object 的數據來自子 class,
父 class 無法訪問子變量
(就像我們使用super();從孩子那里獲取父變量一樣......但我們不能做相反的事情)

 class GcodeAPI { } class Gcommands extends GcodeAPI { constructor() { super(); this.lineOfCode = "⚠️ No line of code - please use getLine() method first"; } getCode() { this.setLastPosition(); this.lineOfCode = `${this.prefix} X${this.x} Y${this.y} Z${this.z}`; return this.lineOfCode; } setLastPosition() { GcodeAPI.previusX = this.x; GcodeAPI.previusY = this.y; GcodeAPI.previusZ = this.z; } } class G0 extends Gcommands { constructor(_xyzObj) { super(); // these line are repetitive this.x = _xyzObj.x?? GcodeAPI.previusX?? 0; this.y = _xyzObj.y?? GcodeAPI.previusY?? 0; this.z = _xyzObj.z?? GcodeAPI.previusZ?? 0; this.prefix = `G0`; } } class G1 extends Gcommands { constructor(_xyzObj) { super(); // these line are repetitive this.x = _xyzObj.x?? GcodeAPI.previusX?? 0; this.y = _xyzObj.y?? GcodeAPI.previusY?? 0; this.z = _xyzObj.z?? GcodeAPI.previusZ?? 0; this.prefix = `G1`; } } class G2 extends Gcommands { constructor(_xyzObj) { super(); // these line are repetitive this.x = _xyzObj.x?? GcodeAPI.previusX?? 0; this.y = _xyzObj.y?? GcodeAPI.previusY?? 0; this.z = _xyzObj.z?? GcodeAPI.previusZ?? 0; this.prefix = `G2`; } } class G3 extends Gcommands { constructor(_xyzObj) { super(); // these line are repetitive this.x = _xyzObj.x?? GcodeAPI.previusX?? 0; this.y = _xyzObj.y?? GcodeAPI.previusY?? 0; this.z = _xyzObj.z?? GcodeAPI.previusZ?? 0; this.prefix = `G3`; } } console.log(new G3({ x: 1, y: 2, z: 3 }).getLine());


基本上,我想要一個類似的代碼,
但我不知道制作的語法是什么
object 參數在子 class
但父 class 中的變量。

 class GcodeAPI {} class Gcommands extends GcodeAPI { constructor() { super(); this.lineOfCode = "⚠️ No line of code - please use getLine() method first"; // here are the repetitive classes this.x = _xyzObj.x?? GcodeAPI.previusX?? 0; this.y = _xyzObj.y?? GcodeAPI.previusY?? 0; this.z = _xyzObj.z?? GcodeAPI.previusZ?? 0; } getCode() { this.setLastPosition(); this.lineOfCode = `${this.prefix} X${this.x} Y${this.y} Z${this.z}`; return this.lineOfCode; } setLastPosition() { GcodeAPI.previusX = this.x; GcodeAPI.previusY = this.y; GcodeAPI.previusZ = this.z; } } class G0 extends Gcommands { constructor(_xyzObj) { super(); this.prefix = `G0`; } } class G1 extends Gcommands { constructor(_xyzObj) { super(); this.prefix = `G1`; } } class G2 extends Gcommands { constructor(_xyzObj) { super(); this.prefix = `G2`; } } class G3 extends Gcommands { constructor(_xyzObj) { super(); this.prefix = `G3`; } }


如果您需要一些測試示例:

 console.log( new G0({ x: 1, y: 2, }).getCode(), ); console.log( new G1({ x: 3, y: 4, }).getCode(), ); console.log( new G2({ x: 5, }).getCode(), ); console.log( new G3({ z: 6, }).getCode(), ); console.log( new G1({ x: 7, y: 8, z: 9, }).getCode(), );

您可以將_xyzObj傳遞給 super 以便它可以訪問。

 class GcodeAPI { constructor(_xyzObj) { this.lineOfCode = "⚠️ No line of code - please use getLine() method first"; this.x = _xyzObj.x?? this.previusX?? 0; this.y = _xyzObj.y?? this.previusY?? 0; this.z = _xyzObj.z?? this.previusZ?? 0; } } class Gcommands extends GcodeAPI { getCode() { this.setLastPosition(); this.lineOfCode = `${this.prefix} X${this.x} Y${this.y} Z${this.z}`; return this.lineOfCode; } setLastPosition() { GcodeAPI.previusX = this.x; GcodeAPI.previusY = this.y; GcodeAPI.previusZ = this.z; } } class G0 extends Gcommands { constructor(_xyzObj) { super(_xyzObj); this.prefix = "G0"; } } class G1 extends Gcommands { constructor(_xyzObj) { super(_xyzObj); this.prefix = "G1"; } } class G2 extends Gcommands { constructor(_xyzObj) { super(_xyzObj); this.prefix = "G2"; } } class G3 extends Gcommands { constructor(_xyzObj) { super(_xyzObj); this.prefix = "G3"; } } console.log( new G0({ x: 1, y: 2, }).getCode(), ); console.log( new G1({ x: 3, y: 4, }).getCode(), ); console.log( new G2({ x: 5, }).getCode(), ); console.log( new G3({ z: 6, }).getCode(), ); console.log( new G1({ x: 7, y: 8, z: 9, }).getCode(), );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM