03. 面向对象
面向对象编程(OOP)是一种以程序创建和管理元素为基础的范式...
03. JavaScript 面向对象编程
Sección titulada «03. Programación Orientada a Objetos en JavaScript»面向对象编程(OOP)是一种以创建和管理独立元素(称为“对象”)为核心的编程范式,这些对象模拟现实世界中的实体。每个对象负责特定任务,并通过对象间的交互推进程序执行。
目前,JavaScript 同时支持传统的“原型”系统和 ES6 引入的现代类语法。
面向对象编程(OOP)是一种以创建和管理独立元素(称为“对象”)为核心的编程范式,这些对象模拟现实世界中的实体。每个对象负责特定任务,并通过对象间的交互推进程序执行。
在 JavaScript 中,OOP 可以通过以下两种方式实现:
- 原型系统:通过创建一个原型对象字面量,其他对象继承该原型。
- 现代类语法(ES6 引入):更接近其他语言的语法,我们推荐使用。
将现实世界中的实体以简化形式表示在代码中的能力。通过定义类来模拟对象的特征(属性)和行为(方法),并隐藏不必要的细节或复杂性。
用于创建对象的模板。通过定义特征和行为来指定对象的结构。
类的实例(由类创建的元素)。代表现实世界中的实体,具有相关特征(属性)和行为(方法)。每个对象可能具有不同的属性值,但共享类中定义的方法。
隐藏对象内部细节(属性和方法)的机制,仅暴露一个接口供交互。通过私有属性和方法控制对对象属性和方法的访问。封装确保数据完整性,并隐藏实现代码供外部使用。
允许从现有类(父类)派生新类(子类)。子类继承父类的属性和方法,也可添加新功能或修改继承行为。继承有助于代码复用并建立类层次结构。
对象对同一操作或方法表现出不同行为的能力(取决于上下文)。同一方法名可根据实现类执行不同任务。此过程通过继承和子类方法重写实现。
构造方法:创建对象时自动执行的特殊方法,用于初始化对象属性。
属性:表示对象特征或状态的变量。可以是数字、布尔值、字符串、数组、其他对象等数据类型。
方法:定义对象行为的函数。可接收输入参数、返回结果并操作对象属性。
// Declaració d'una classeclass Jugador { constructor(nom, vides) { // mètode constructor this.nom = nom; // propietat this.vides = vides; this.posicio = 100; this.punts = 0; }
moureJugador() { // mètode públic this.posicio += 5; }}我们将执行代码以实例化和管理对象。
// Instanciació d'una classeconst jugador1 = new Jugador('Pepet', 3);const jugador2 = new Jugador('Lucky', 5);
// Accés a propietats i ús de mètodesconsole.log(`Nom Jugador 1:${jugador1.nom}`);console.log(`Nom Jugador 2:${jugador2.nom}`);
jugador1.moureJugador();jugador1.vides--;
for (let i = 0; i < 5; i++) { jugador2.moureJugador();}
console.log(`Vides Jugador 1:${jugador1.vides}`);console.log(`Posicio Jugador 2:${jugador2.posicio}`);我们将继承和多态建立在我们的类中。
// Classe base que representa un personatge del jocclass Entitat { constructor(nom, vides, posicio) { this.nom = nom; this.vides = vides; this.posicio = posicio; }
moure() { this.posicio.x += 1; this.posicio.y += 1; }}
// Herència: Jugador hereta propietats i mètodes d'Entitatclass Jugador extends Entitat { constructor(nom, vides, posicio) { super(nom, vides, posicio); // crida al constructor de la classe base this.punts = 0; // propietat específica de Jugador }
// Polimorfisme: Jugador redefineix el mètode moure() de la classe base moure() { this.posicio.y += 1; // Moviment vertical }
// Mètode propi del Jugador sumarPunts(enemic) { this.punts += 10; enemic.vides--; }}
// Herència: Enemic hereta propietats i mètodes d'Entitatclass Enemic extends Entitat { constructor(nom, vides, nivell) { super(nom, vides); this.nivell = nivell; // propietat específica de Enemic }
// Polimorfisme: Enemic redefineix el mètode moure() de la classe base moure() { this.posicio.x -= 1; // Moviment horitzontal }
// Mètode propi de l'Enemic atacar(jugador) { jugador.vides--; jugador.punts = jugador.punts - this.nivell; }}
const jugador = new Jugador('Jugador1', 3, { x: 100, y: 100 });const enemic = new Enemic('Enemic1', 2, { x: 300, y: 500 }, 1);
jugador.moure();enemic.moure();
jugador.sumarPunts(enemic);enemic.atacar(jugador);
console.log(`Vides Jugador: ${jugador.vides} i Posició: ${jugador.posicio.y}`);console.log(`Vides Enemic: ${enemic.vides} i Posició: ${enemic.posicio.x}`);
