
本文共 3470 字,大约阅读时间需要 11 分钟。
面向对象编程(OOP)与 TypeScript 类
一、面向对象编程的基础概念
面向对象编程(Object-Oriented Programming, OOP)是一种以对象为中心的编程 paradigm,核心理念是将实物世界抽象为对象,通过对象的数据和行为来描述问题。OOP 的三大核心概念是 封装、继承 和 多态。
1.1 封装(Encapsulation)
封装的核心思想是将对象的属性和方法隐藏起来,只暴露外部需要的接口。通过这种方式,可以保护对象的内部逻辑不被随意访问,同时确保外部代码无法修改对象的状态。例如,银行账户类可能会封装账户余额和交易记录,只允许通过 getBalance()
和 deposit()
方法进行操作。
1.2 继承(Inheritance)
继承允许一个新类(子类)继承已有的类(父类)的属性和方法。子类可以选择性地覆盖父类的方法或添加新的方法,从而扩展功能。例如,Dog
类可以继承 Animal
类,增加与狗相关的特性,如 bark()
方法。
1.3 多态(Polymorphism)
多态是指同一方法可以在不同对象中有不同的行为。通过继承关系,子类可以为父类的方法提供特定的实现。例如,Cat
和 Dog
都继承自 Animal
,但它们的 eat()
方法可以有不同的行为。
二、TypeScript 类的基础
TypeScript 是 JavaScript 的超集,支持面向对象编程,能够定义类、接口等概念。类是用来描述对象的模板,它包含对象的共同属性和方法。
2.1 类的定义
使用 class
关键字定义类,类名后面跟着属性或方法的定义。类的核心用法包括定义字段(属性)、构造函数(用于实例化)和方法。
class Animal { name: string; constructor(name: string) { this.name = name; } sayHi(): string { return `我的名字是 ${this.name}`; }}// 创建实例const cat = new Animal('Tom');console.log(cat.sayHi()); // 输出:我的名字是 Tom
2.2 类的继承
类可以继承其他类,使用 extends
关键字。子类可以选择性地覆盖父类的属性或方法。
class Cat extends Animal { color: string; constructor(name: string, color: string) { super(name); this.color = color; } sayHi(): string { return super.sayHi() + `我是一只${this.color}色的猫`; }}// 创建实例const cat2 = new Cat('Tom', '橘黄');console.log(cat2.sayHi()); // 输出:我的名字是 Tom我是一只橘黄色的猫
2.3 存取器(Getter & Setter)
通过 getter
和 setter
,可以自定义属性的读取和赋值行为。getter
定义属性的读取方式,setter
定义赋值方式。
class Animal2 { private userName: string; constructor(name: string) { this.userName = name; } get userName(): string { return 'muzidigbig'; } set userName(value: string) { console.log('setter: ', value); }}// 创建实例const a = new Animal2('Kitty');console.log(a.userName); // muzidigbig
2.4 修饰符(Modifiers)
修饰符用于限定属性或方法的访问范围。public
是默认访问级别,private
仅在类内部可用,protected
在类及其子类中可用。
class Person { public name: string; private idCard: number; protected phone: number; constructor(name: string, idCard: number, phone: number) { this.name = name; this.idCard = idCard; this.phone = phone; }}// 创建实例const tom = new Person('Tom', 42000, 13986358544);console.log(tom.name); // Tom
三、多态与抽象类
3.1 多态
多态允许同一方法在不同对象中有不同的行为。例如,Person
类和其子类 Student
可以共享一个 study()
方法,但每个子类可以有不同的实现。
class Person { study(): void { console.log('学习'); }}class Student extends Person { study(): void { console.log('学习课程'); }}// 创建实例const person: Person = new Person();const student: Student = new Student();person.study(); // 输出:学习student.study(); // 输出:学习课程
3.2 抽象类与抽象方法
抽象类是不能被实例化的类,通常用于提供其他类继承的基础。抽象方法必须在子类中实现。
abstract class Animal { abstract eat(): void; sleep(): void { console.log('睡觉'); }}class Cat extends Animal { eat(): void { console.log('猫在吃'); }}// 创建实例const cat = new Cat();// 会报错,因为 Cat 没有实现 eat 方法
四、类与接口
4.1 接口(Interfaces)
接口用于定义对象的公共属性和方法,类可以实现接口,从而提高代码的灵活性和可维护性。
interface Ieat { eat(): void;}class Person implements Ieat { eat(): void { console.log('人在吃'); }}class Animal implements Ieat { eat(): void { console.log('动物在吃'); }}
4.2 类实现接口
类可以实现多个接口,提高代码的复用性和扩展性。
interface Iperson { name: string; age: number;}interface Istudent { grade: number;}class Student implements Iperson, Istudent { name: string; age: number; grade: number; constructor(name: string, age: number, grade: number) { this.name = name; this.age = age; this.grade = grade; }}// 创建实例const student = new Student('张三', 18, 90);console.log(student.name); // 张三
发表评论
最新留言
关于作者
