TypeScript 支持的面向对象术语
面向对象编程 (OOP) 是一种流行的编程范式,已广泛应用于软件开发行业。OOP 基于对象的理念,对象是类的实例,封装了数据和行为。TypeScript 是 JavaScript 的静态类型超集,旨在支持大型应用程序的开发,它也是一种面向对象编程语言。在本文中,我们将探讨 TypeScript 支持的面向对象术语。
下面我们将讨论 TypeScript 支持的各种面向对象术语。
类
在 TypeScript 中,类是创建对象的蓝图,它定义了一组对该类所有实例都通用的属性和方法。类使用class关键字声明,后跟类名和包含类定义的一组花括号。
语法
在 TypeScript 中声明类的语法如下:
class ClassName {
// properties and methods
}
示例 1
这里我们声明了一个Car类,它包含make、model和year属性以及它们对应的 getter 和 setter 方法。
class Car {
private make: string;
private model: string;
private year: number;
constructor(make: string, model: string, year: number) {
this.make = make;
this.model = model;
this.year = year;
}
public getMake(): string {
return this.make;
}
public setMake(make: string): void {
this.make = make;
}
public getModel(): string {
return this.model;
}
public setModel(model: string): void {
this.model = model;
}
public getYear(): number {
return this.year;
}
public setYear(year: number): void {
this.year = year;
}
}
对象
在 TypeScript 中,对象是类的实例,拥有自己的一组属性和方法。对象使用new关键字创建,后跟类名和类构造函数所需的任何参数。
语法
创建类实例(对象)的语法:
let objectName = new ClassName(arguments);
示例 2
这将创建一个名为myCar的对象,其中 make: Toyota,model: Camry,year: 2022。
let myCar = new Car('Toyota', 'Camry', 2022);
console.log(myCar.getMake(), myCar.getModel(), myCar.getYear());
构造函数
在 TypeScript 中,构造函数是一种特殊的方法,在从类创建对象时调用。构造函数用于使用默认值初始化对象。构造函数方法使用constructor关键字声明,后跟任何必要的参数。
语法
在 TypeScript 中编写构造函数的语法:
class ClassName {
constructor(arguments) {
// initialization code
}
}
示例 3
这定义了一个具有空主体和三个参数的构造函数,即:make、model和year。
class Car {
constructor(private make: string, private model: string, private year: number) {}
}
继承
在 TypeScript 中,继承是一种机制,允许一个类继承另一个类的属性和方法。继承自另一个类的类称为子类或派生类,被继承的类称为超类或基类。在 TypeScript 中,继承使用extends关键字声明。
语法
从父类(超类)创建子类(子类)的语法:
class SubclassName extends SuperclassName {
// additional properties and methods
}
示例 4
这里子类:ElectricCar继承了父类:Car的属性和方法。此外,它还有自己的属性range及其 getter 和 setter 方法,这些方法在父类中不存在。
class ElectricCar extends Car {
private range: number;
constructor(make: string, model: string, year: number, range: number) {
super(make, model, year);
this.range = range;
}
public getRange(): number {
return this.range;
}
public setRange(range: number): void {
this.range = range;
}
}
const tesla = new ElectricCar("Tesla", "Model S", 2019, 300);
console.log(
tesla.getMake(),
tesla.getModel(),
tesla.getYear(),
tesla.getRange()
);
多态性
在 TypeScript 中,多态性是指对象能够采用多种形式或类型的功能,允许不同类的对象被视为公共类的对象。多态性是通过继承和接口实现的。这是一个演示多态性的示例
示例 5
这里,我们首先创建了一个接口Shape,它类似于 TypeScript 中的类。它有一个声明的方法area。此接口被实现了两次,一次用于Circle类,另一次用于Rectangle类。在这两个类中,我们都定义了area函数的主体,其定义方式不同,但函数名称完全相同。这演示了 OOP 中的多态性(相同名称但不同形式)。
interface Shape {
area(): number;
}
class Circle implements Shape {
constructor(private radius: number) {}
public area(): number {
return Math.PI * this.radius ** 2;
}
}
class Rectangle implements Shape {
constructor(private width: number, private height: number) {}
public area(): number {
return this.width * this.height;
}
}
let shapes: Shape[] = [new Circle(5), new Rectangle(10, 20)];
shapes.forEach(shape => console.log(shape.area()));
编译后,将生成以下 JavaScript 代码:
var Circle = /** @class */ (function () {
function Circle(radius) {
this.radius = radius;
}
Circle.prototype.area = function () {
return Math.PI * Math.pow(this.radius, 2);
};
return Circle;
}());
var Rectangle = /** @class */ (function () {
function Rectangle(width, height) {
this.width = width;
this.height = height;
}
Rectangle.prototype.area = function () {
return this.width * this.height;
};
return Rectangle;
}());
var shapes = [new Circle(5), new Rectangle(10, 20)];
shapes.forEach(function (shape) { return console.log(shape.area()); });
输出
以上代码将产生以下输出:
78.53981633974483 200
接口
在 TypeScript 中,接口是创建对象的蓝图,它定义了一组必须由实现该接口的任何对象实现的属性和方法。接口使用interface关键字声明,后跟接口名和包含接口定义的一组花括号。
语法
interface InterfaceName {
// properties and methods
}
示例 6
interface Person {
firstName: string;
lastName: string;
age: number;
}
class Employee implements Person {
constructor(public firstName: string, public lastName: string, public age: number, public jobTitle: string) {}
}
let Employee: Person = new Employee('John', 'Doe', 30, 'Software Engineer');
console.log(employee.firstName, employee.lastName, employee.age);
编译后,将生成以下 JavaScript 代码:
var Employee = /** @class */ (function () {
function Employee(firstName, lastName, age, jobTitle) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.jobTitle = jobTitle;
}
return Employee;
}());
var employee = new Employee('John', 'Doe', 30, 'Software Engineer');
console.log(employee.firstName, employee.lastName, employee.age);
输出
以上代码将产生以下输出:
John Doe 30
这里我们定义了一个名为Person的接口,并且此接口使用名为Employee的类实现。此外,我们通过在构造函数中传递参数来创建此类的实例。
结论
总而言之,TypeScript 对面向对象编程的支持允许开发人员创建结构良好且易于维护的代码。该语言的功能,包括类、对象、继承、多态性、接口等,使编写和维护复杂的应用程序变得更容易。因此,TypeScript 已经成为构建 Web 应用程序的热门选择,特别是那些需要高度组织和结构的应用程序。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP