JS自体がオブジェクト指向開発のプログラミング言語がベース
クラス:カプセル化、継承、ポリモーフィズム
- 1.カプセル化:クラスも関数であり、コードの関数の実装は、 "低結合と高結合 "を達成するために、カプセル化されます。
- 2.ポリモーフィズム:オーバーロード、上書き
- 2.1 オーバーライド: サブクラスが親クラスのメソッドをオーバーライドすること。
- 2.2 オーバーロード:同じメソッドでも、パラメータや戻り値が異なるため、異なる機能を持つこと。
- 3.継承:サブクラスは親クラスからメソッドを継承します。
オーバーロード
1.バックエンド言語の実行順序
public void fn(int x,init y){ } public void fn(int x){ } //最初のFN fn(10,20); // 第二の継承を実行するFN fn(10); // fn(' ');2. js言語には、厳密な意味でのオーバーロードはありません。
function fn(x, y) { } function fn(x) { } //第二の継承を実行するFN fn(10, 20); //第二の継承を実行するFN fn(10);3.JS pass parameter y===undefined, yは何かをするために存在しません。
function fn(x, y) { if(y===undefined){ // ...y=0; return; } // .... } //y===undefined,y何かをするということはない fn(10); fn(10, 20);
継承
- 1.JSでは、継承は他のプログラミング言語と同じではありません。
- 2.継承の目的:サブクラスのインスタンスにも親クラスのprivate属性やpublicメソッドを持たせること。
1. 最初の継承スキームのJS:プロトタイプ継承
1. サブクラスのインスタンス、プロトタイプでサブクラスのprivateとpublicを使用可能。
2.親クラスのインスタンスで、親クラスのprivateとpublicをプロトタイプで使用できます。
プロトタイプ継承の特徴
1.親クラスのプライベート属性と共有メソッドは、クラスインスタンスで共有されます。
2.他の言語とは異なり、プロトタイプ継承は、親クラスのプロパティやメソッドを子クラスに「コピー」するのではなく、子クラスのインスタンスが__proto__プロトタイプチェーンに基づいて定義された独自のプロパティやメソッドを見つけることができます。
3.C1__proto___.xxx=xxx は、サブクラスのプロトタイプの内容を変更します。このプロトタイプが変更されると、サブクラスの他のインスタンスには影響しますが、親クラスのインスタンスには影響しません。
4.C1__proto___.proto.xxx=xxx は、親のプロトタイプを直接変更します。これは、他の親のインスタンスに影響するだけでなく、サブクラスのインスタンスの書き込みにも影響します。
function Parent() { this.x = 100; } Parent.prototype.getX = function getX() { return this.x; }; function Child() { this.y = 200; } //サブクラスのインスタンスにも、親クラスのprivateプロパティやpublicメソッドを持たせる。 Child.prototype = new Parent; //=>プロトタイプ継承 Child.prototype.getY = function getY() { return this.y; }; let c1 = new Child; console.log(c1);
2.第二の相続のJS:CALL相続
//C1xとgetX
function Parent() {
this.x = 100;
}
Parent.prototype.getX = function getX() {
return this.x;
};
function Child() {
// サブクラスのコンストラクタで、親クラスを通常のメソッドとして実行する
// this -> Child c1
// this.x=100 これは、インスタンスc1にprivate属性xを強制することと等価である。
//これは、サブクラスのインスタンスに親クラスのプライベート属性を継承させるのと同じことである。
//また、サブクラスのプライベート・プロパティ "コピー・タイプ "も変更された。
Parent.call(this);
this.y = 200;
}
Child.prototype.getY = function getY() {
return this.y;
};
let c1 = new Child;
console.log(c1);
3.第3の遺伝:寄生的組み合わせ遺伝におけるJS
//C1xとgetX
function Parent() {
this.x = 100;
}
Parent.prototype.getX = function getX() {
return this.x;
};
function Child() {
//1.これをparent
Parent.call(this);
this.y = 200;
}
//2.子クラスのプロトタイプを親クラスのプロトタイプと等しくする
Child.prototype = Object.create(Parent.prototype);
// 3.プロトタイプを破棄し、独自のプロトタイプを作成し、プロトタイプの整合性を確保するために、すべて手作業でクストラクタを追加する。
Child.prototype.constructor = Child;
Child.prototype.getY = function getY() {
return this.y;
};
let c1 = new Child;
console.log(c1);
// を指すプロトタイプ・チェインを持つ空のオブジェクトを作成する。obj
let obj = {
xxx: 'xxx'
};
console.log(Object.create(obj));
4.ES6におけるクラスと継承
//C1xとgetX
class Parent {
constructor() {
this.x = 100;
}
//1.ネイティブに似ている Parent.prototype.getX=function...
getX() {
return this.x;
}
}
// 2. : extends Parent
// 3.注 CONSTRUCTORの最初の行に継承を追加する必要がある。SUPER
class Child extends Parent {
constructor() {
//=>4.super();CALL継承に似ている
//super():
//Parentのコンストラクタの実行に等しい。
super();
this.y = 200;
}
getY() {
return this.y;
}
}
let c1 = new Child;
console.log(c1);
// Child();
//=>Uncaught TypeError: Class constructor Child cannot be invoked without 'new'
//ES6ES6では、クラスの作成はクラスであり、通常の関数として実行することはできない。





