返回
深入理解《红宝书》第8章:类及其应用
前端
2023-12-06 12:12:06
1. 类的基本概念
类(Class)是ECMAScript中的一种新语法,它是对原型和构造函数概念的封装。类提供了一种简便的方法来创建对象实例,并对对象进行封装、继承和多态等操作。
2. 类实例化
我们可以通过使用new来实例化类,这将创建一个新的对象,该对象具有类的所有属性和方法。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person = new Person('John', 30);
3. 类属性和方法
类属性和方法可以通过在类定义中使用关键字this
来定义。类属性是与类关联的数据,而类方法是与类关联的函数。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const person = new Person('John', 30);
person.speak(); // 输出: "Hello, my name is John and I am 30 years old."
4. 类继承
类继承是允许一个类从另一个类继承属性和方法。子类可以访问父类中的所有属性和方法,并可以重写父类中的方法。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
class Student extends Person {
constructor(name, age, school) {
super(name, age); // 调用父类构造函数
this.school = school;
}
study() {
console.log('I am studying.');
}
}
const student = new Student('John', 30, 'Harvard University');
student.speak(); // 输出: "Hello, my name is John and I am 30 years old."
student.study(); // 输出: "I am studying."
5. 类多态
类多态是允许子类中的方法与父类中的方法具有不同的实现。这使得子类可以根据自己的需要定制方法的行为。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
class Student extends Person {
constructor(name, age, school) {
super(name, age); // 调用父类构造函数
this.school = school;
}
speak() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old. I am a student at ${this.school}.`);
}
}
const person = new Person('John', 30);
const student = new Student('John', 30, 'Harvard University');
person.speak(); // 输出: "Hello, my name is John and I am 30 years old."
student.speak(); // 输出: "Hello, my name is John and I am 30 years old. I am a student at Harvard University."
6. 类封装
类封装是将数据和操作封装在一个对象中,以实现信息的隐藏和保护。类中的属性和方法可以通过访问权限修饰符来控制访问权限,如public、protected和private。
class Person {
constructor(name, age) {
this._name = name; // 私有属性
this.age = age; // 公有属性
}
speak() {
console.log(`Hello, my name is ${this._name} and I am ${this.age} years old.`);
}
get name() {
return this._name; // 获取私有属性值
}
set name(value) {
this._name = value; // 设置私有属性值
}
}
const person = new Person('John', 30);
person.speak(); // 输出: "Hello, my name is John and I am 30 years old."
person.name = 'Jane'; // 设置私有属性值
person.speak(); // 输出: "Hello, my name is Jane and I am 30 years old."
7. 类抽象
类抽象是允许类定义一些抽象方法,这些方法没有实现,子类必须实现这些方法。抽象类不能被实例化,只能被继承。
abstract class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
abstract speak(); // 抽象方法
get name() {
return this._name; // 获取私有属性值
}
set name(value) {
this._name = value; // 设置私有属性值
}
}
class Student extends Person {
constructor(name, age, school) {
super(name, age); // 调用父类构造函数
this.school = school;
}
speak() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old. I am a student at ${this.school}.`);
}
}
const person = new Person('John', 30); // 报错: 不能实例化抽象类
const student = new Student('John', 30, 'Harvard University');
student.speak(); // 输出: "Hello, my name is John and I am 30 years old. I am a student at Harvard University."
8. 类应用
类在实际开发中有着广泛的应用,如:
- 创建可重用组件
- 构建面向对象系统
- 实现抽象和多态
- 提高代码的可读性和可维护性
9. 结语
类是ECMAScript中的一种新的基础性语法糖结构,它通过封装、继承和多态等特性,为开发者提供了构建复杂应用程序的利器。通过本篇文章的学习,相信读者已经对类有了更深入的理解,并能够在实际开发中熟练地使用类。