最終更新日 2024-09-25
~文法メモ~ クラスの継承
「extends」と「super」
ECMAScript6では「クラスの継承」もとても自然な記述で行うことが出来ます。
継承は「extends」キーワードで利用することが出来ます。
子クラスから直接の親クラスのコンストラクタは、「super」で呼び出すことが出来ます。
継承したクラスで、ウィンドウを移動するmoveメソッドを追加してみましょう。
class BaseWindow {
constructor(x, y, w, h) {
this.title = "ベースのクラスのデフォのタイトル",
this.x = x
this.y = y
this.w = w
this.h = h
}
show() {
return `タイトル:${this.title}, x:${this.x}, y:${this.y}, w:${this.w}, h:${this.h}`;
}
}
class MyWindow extends BaseWindow {
constructor(x, y, w, h) {
super(x, y, w, h);
this.title = "僕のクラスのタイトル";
}
move(x, y) {
this.x = x
this.y = y
}
}
let win = new MyWindow(10, 20, 500, 300);
hm.debuginfo(win.show());
win.move(50, 100);
hm.debuginfo(win.show());
.NETのクラスを、ECMAScriptのクラスで継承することはできない
hmV8/ClearScriptでは、.NETの各種クラスを利用することが出来ますが、
これらのクラスをECMAScriptのクラスで継承することは出来ません。
// .NETアセンブリの読み込み
lib = host.lib("System.Windows.Forms");
class MyForm extends lib.System.Windows.Forms.Form { // エラー。これは出来ない。
constructor(start) {
}
}