注意: 如果您是 TypeScript 新手,请先查看我们的 TypeScript 入门 教程。
构造函数是类的一个特殊方法,在您使用 `new` 关键字创建该类的新实例时会自动调用。
它主要用于初始化新对象的属性。
这是一个简单的类构造函数示例。您可以阅读其余教程了解更多信息。
示例
class Student {
name: string;
constructor(name: string) {
this.name = name; // Initialize the property
console.log("Constructor is called");
}
greet(): string {
return `Welcome, ${this.name}!`;
}
}
// Create an instance of Student class
// Pass "Leon Kennedy" as argument to the constructor
let student1 = new Student("Leon Kennedy");
console.log(student1.greet());
// Output:
// Constructor is called
// Welcome, Leon Kennedy!
在这里,当我们在创建 `student1` 对象时,`constructor()` 会被自动调用。它将 `name` 属性初始化为 `"Leon Kennedy"`。
创建构造函数
我们使用 `constructor` 关键字在类中创建构造函数。以下是创建构造函数的语法:
class ClassName {
constructor(parameters) {
// Initialization code
}
}
这里,
ClassName
- 您正在创建构造函数的类的名称。constructor
- 用于创建构造函数的关键字。此关键字也充当构造函数方法的名称。parameters
- 用于初始化类属性的构造函数参数。
注意事项
- 构造函数方法必须始终命名为 `constructor`。
- 一个类只能有一个构造函数。
示例 1:TypeScript 构造函数方法
class Student {
name: string;
gpa: number;
constructor(name: string, gpa: number) {
// Initialize the properties
this.name = name;
this.gpa = gpa;
}
}
// Create an instance of Student class
// Pass "Leon Kennedy" and 3.8 as arguments to its constructor
let student1 = new Student("Leon Kennedy", 3.8);
// Print the name and gpa properties of student1
console.log(`Name: ${student1.name}`);
console.log(`GPA: ${student1.gpa}`);
输出
Name: Leon Kennedy GPA: 3.8
在此,`Student` 类有两个属性:`name` 和 `gpa`。
我们的构造函数方法通过将参数的值赋给相应的类属性来初始化这些属性。
constructor(name: string, gpa: number) {
// Initialize the properties
this.name = name;
this.gpa = gpa;
}
在构造函数方法内部,
name
和gpa
- 构造函数的参数。this.name
和this.gpa
- 我们在类(构造函数上方)中声明的实例属性。更具体地说,`this.name` 和 `this.gpa` 指的是属于类每个独立实例的属性。
定义了我们的类后,我们通过将其构造函数所需的参数传递给 `Student` 类(`student1`)来创建 `Student` 类的一个实例。
let student1 = new Student("Leon Kennedy", 3.8);

示例 2:创建多个实例
现在,让我们重写之前的程序,创建 `Student` 类的两个实例。
我们还将创建一个 `printInfo()` 方法来打印对象的 `name` 和 `gpa` 属性。
class Student {
name: string;
gpa: number;
constructor(name: string, gpa: number) {
// Initialize the properties
this.name = name;
this.gpa = gpa;
}
// Method to print student details
printInfo(): void {
console.log(`Name: ${this.name}`);
console.log(`GPA: ${this.gpa}`);
console.log();
}
}
// Create an instance of Student class
// Pass "Leon Kennedy" and 3.8 as arguments to its constructor
let student1 = new Student("Leon Kennedy", 3.8);
// Create another instance of Student class
// Pass "Ada Wong" and 3.6 as arguments to its constructor
let student2 = new Student("Ada Wong", 3.6);
// Call the printInfo() method of the two instances
student1.printInfo();
student2.printInfo();
输出
Name: Leon Kennedy GPA: 3.8 Name: Ada Wong GPA: 3.6
在这里,我们通过将其构造函数所需的参数传递给 `Student` 类(`student1` 和 `student2`)创建了 `Student` 类的两个实例。
let student1 = new Student("Leon Kennedy", 3.8);
let student2 = new Student("Ada Wong", 3.6);
1. 创建 student1 时。
- `name` 参数的值为 `"Leon Kennedy"`。
- `gpa` 参数的值为 3.8。
2. 创建 student2 时。
- `name` 参数的值为 `"Ada Wong"`。
- `gpa` 参数的值为 3.6。
构造函数参数属性(简写)
您还可以通过添加可见性修饰符(例如 `public`、`private`、`protected` 或 `readonly`)直接在构造函数参数中声明和初始化属性。例如,
class Student {
constructor(public name: string, private gpa: number) {}
getGPA(): number {
return this.gpa;
}
}
let student1 = new Student("Leon Kennedy", 3.8);
console.log(`Name: ${student1.name}`);
console.log(`GPA: ${student1.getGPA()}`);
输出
Name: Leon Kennedy GPA: 3.8
正如您所见,我们在构造函数中声明并初始化了两个属性。
public name: string
- 声明并初始化一个 `public` 属性 `name`。private gpa: number
- 声明并初始化一个 `private` 属性 `gpa`。
注意:使用此简写可以减少样板代码。
上面的程序等同于下面的程序。
class Student {
public name: string;
private gpa: number;
constructor(name: string, gpa: number) {
this.name = name;
this.gpa = gpa;
}
getGPA(): number {
return this.gpa;
}
}
let student1 = new Student("Leon Kennedy", 3.8);
console.log(`Name: ${student1.name}`);
console.log(`GPA: ${student1.getGPA()}`);
更多关于 TypeScript 构造函数
您还可以为构造函数参数提供 默认值。
class Student {
constructor(public name: string = "Unknown", public gpa: number = 0) {}
}
// Don't pass any value to the constructor
let student1 = new Student();
// Pass values to the constructor
let student2 = new Student("Jill Valentine", 4);
console.log(`Student1 Name: ${student1.name}`);
console.log(`Student1 GPA: ${student1.gpa}`);
console.log(`Student2 Name: ${student2.name}`);
console.log(`Student2 GPA: ${student2.gpa}`);
输出
Student1 Name: Unknown Student1 GPA: 0 Student2 Name: Jill Valentine Student2 GPA: 4
如果未传递参数,则使用默认值。
当您将一个类派生自另一个类时,派生类必须在其构造函数中调用 `super()` 来调用基类的构造函数。
class Person {
constructor(public name: string) {}
}
class Student extends Person {
constructor(name: string, public gpa: number) {
// Call constructor of Person class
super(name);
}
getInfo(): string {
return `${this.name} has a GPA of ${this.gpa}`;
}
}
let student1 = new Student("Leon Kennedy", 3.8);
console.log(student1.getInfo());
// Output: Leon Kennedy has a GPA of 3.8
在这里,`Student` 类派生自 `Person` 类。因此,`Student` 对象可以访问 `Person` 的属性和构造函数。
在 `Student` 类中,我们在其 `constructor()` 方法中使用 `super()` 来访问 `Person` 类的构造函数。这确保 `name` 属性也得到初始化。
阅读更多