类和继承

1. 概述

在面向对象编程(OOP)中,类(Class)是一种用于创建对象的蓝图或模板。类定义了对象的属性和方法,而继承(Inheritance)则允许一个类继承另一个类的属性和方法,从而实现代码的重用和扩展。

JavaScript 是一种基于原型的语言,但在 ES6 中引入了 class 关键字,使得类的定义和继承更加直观和易于理解。

2. 类的定义

在 JavaScript 中,可以使用 class 关键字来定义一个类。类中可以包含构造函数(constructor)、属性和方法。

2.1 基本语法

class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        console.log(`${this.name} makes a noise.`);
    }
}

// 创建对象
const dog = new Animal('Dog');
dog.speak(); // 输出: Dog makes a noise.

2.2 构造函数

构造函数(constructor)是一个特殊的方法,用于初始化对象的属性。每个类只能有一个构造函数。

class Animal {
    constructor(name) {
        this.name = name;
    }
}

2.3 方法

类中的方法可以直接定义在类内部,并且可以通过对象实例调用。

class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        console.log(`${this.name} makes a noise.`);
    }
}

3. 继承

继承允许一个类继承另一个类的属性和方法。在 JavaScript 中,可以使用 extends 关键字来实现继承。

3.1 基本语法

class Dog extends Animal {
    constructor(name) {
        super(name); // 调用父类的构造函数
    }

    speak() {
        console.log(`${this.name} barks.`);
    }
}

const dog = new Dog('Rex');
dog.speak(); // 输出: Rex barks.

3.2 super 关键字

super 关键字用于调用父类的构造函数和方法。在子类的构造函数中,必须先调用 super() 才能使用 this。

class Dog extends Animal {
    constructor(name) {
        super(name); // 调用父类的构造函数
    }

    speak() {
        super.speak(); // 调用父类的方法
        console.log(`${this.name} barks.`);
    }
}

const dog = new Dog('Rex');
dog.speak(); // 输出: Rex makes a noise. Rex barks.

4. 实践练习

4.1 创建一个 Car 类

创建一个 Car 类,包含以下属性和方法:

  • make:汽车品牌
  • model:汽车型号
  • year:生产年份
  • start():启动汽车
  • stop():停止汽车
class Car {
    constructor(make, model, year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    start() {
        console.log(`The ${this.year} ${this.make} ${this.model} is starting.`);
    }

    stop() {
        console.log(`The ${this.year} ${this.make} ${this.model} is stopping.`);
    }
}

const myCar = new Car('Toyota', 'Camry', 2020);
myCar.start(); // 输出: The 2020 Toyota Camry is starting.
myCar.stop();  // 输出: The 2020 Toyota Camry is stopping.

4.2 创建一个 ElectricCar 类继承 Car 类

创建一个 ElectricCar 类,继承 Car 类,并添加以下方法:

  • charge():充电
class ElectricCar extends Car {
    constructor(make, model, year) {
        super(make, model, year);
    }

    charge() {
        console.log(`The ${this.year} ${this.make} ${this.model} is charging.`);
    }
}

const myElectricCar = new ElectricCar('Tesla', 'Model S', 2021);
myElectricCar.start(); // 输出: The 2021 Tesla Model S is starting.
myElectricCar.charge(); // 输出: The 2021 Tesla Model S is charging.
myElectricCar.stop();  // 输出: The 2021 Tesla Model S is stopping.

5. 总结

类和继承是面向对象编程中的重要概念。通过类,我们可以创建具有相同属性和方法的对象;通过继承,我们可以重用和扩展已有的代码。JavaScript 中的 class 和 extends 关键字使得类的定义和继承更加直观和易于理解。

通过本教程的学习,你应该能够理解如何定义类、创建对象以及实现继承。希望你能通过实践练习进一步巩固这些概念。