如何在 TypeScript 中调用特定类的方法?
在本教程中,用户将学习如何在 TypeScript 中调用特定类的方法。类是面向对象编程的基本概念。简单来说,它包含成员变量和方法,我们可以通过创建该特定类的对象来访问它们。因此,类是我们为该特定类创建的对象的蓝图。
类可以包含 TypeScript 中的函数,我们也可以在方法中调用它们。因此,用户需要学习如何访问和调用特定类的方法。
在 TypeScript 中调用特定类的方法
在 TypeScript 中,我们可以创建特定类的对象实例。用户可以使用点运算符,通过引用该类的对象来访问该类的公共成员和方法。
语法
用户可以按照以下语法学习如何创建类的对象,以及如何使用对象和点运算符访问该类的方法。
// 创建类 class Getsum { doSum(multiplier: number): number { // 执行一些操作 return value; } } var object: Getsum = new Getsum(); let result = object.doSum(10);
在上述语法中,我们只是创建了包含该方法的类。此外,我们还定义了该类的对象实例,并使用点运算符访问了 doSum() 方法。
示例
在下面的示例中,我们创建了"GetSum"类。我们还将数字类型的 num1 和 num2 定义为类成员,并分别用 10 和 20 的值初始化它们。
接下来,我们定义了 doSum() 方法,该方法接受"乘数"作为参数并返回数值。在该方法内部,我们使用"this"关键字访问 num1 和 num2,并将两者与乘数相乘。之后,我们对 num1 和 num2 的新值进行求和,将其存储在"sum"变量中,然后返回它。
// 创建类 class Getsum { // 定义类的成员变量 num1: number = 10; num2: number = 20; // Method to get sum of two numbers doSum(multiplier: number): number { // 将 num1 和 num2 乘以乘数 this.num1 = this.num1 * multiplier; this.num2 = this.num2 * multiplier; // 对新的 num1 和 num2 求和,这是我们乘以乘数后得到的 let sum = this.num1 + this.num2; return sum; } } // 创建 Getsum 类的对象 var object: Getsum = new Getsum(); // 调用 doSum() 方法 let result = object.doSum(10); console.log("Result we get after calling the doSum method is " + result);
编译后,它将生成以下 JavaScript 代码 -
// Creating the class var Getsum = /** @class */ (function () { function Getsum() { // 定义类的成员变量 this.num1 = 10; this.num2 = 20; } // Method to get sum of two numbers Getsum.prototype.doSum = function (multiplier) { // 将 num1 和 num2 乘以乘数 this.num1 = this.num1 * multiplier; this.num2 = this.num2 * multiplier; // do sum of new num1 and num2, which we got after multiplying with the multiplier var sum = this.num1 + this.num2; return sum; }; return Getsum; }()); // 创建 Getsum 类的对象 var object = new Getsum(); // 调用 doSum() 方法 var result = object.doSum(10); console.log("调用 doSum 方法后得到的结果是 " + result);
输出
上述代码将产生以下输出 -
调用 doSum 方法后得到的结果是 300
在一个类中调用另一个类的方法
在 TypeScript 中,我们可以在另一个类中访问并调用一个类的公共方法。有时,我们需要在特定类中调用其他类的小方法。
语法
用户可以按照以下语法在一个类中调用另一个类的方法。
class Getsum { doSum(): number { // 执行一些操作 } } class Multiplier { object: Getsum = new Getsum(); result = this.object.doSum(); multiply(): number { // 执行一些操作 } } let multiplierObject: Multiplier = new Multiplier(); let result = multiplierObject.multiply()
在上述语法中,我们创建了两个不同的类。我们在第二个类中调用了第一个类的方法,并在第二个类的另一个方法中使用了它的返回值。
示例
在下面的示例中,GetSum 类包含 doSum() 方法作为成员函数,该方法返回数值。在 Multiplier 类中,我们创建了 GetSum 类的对象,并通过将该对象作为引用并将其返回值存储在结果中来调用 GetSum 类的 doSum() 方法。
此外,我们在 Multiplier 类中创建了 multiply 方法,该方法将结果乘以 30 并返回乘积值。最后,我们创建了 Multiplier 类的对象,并通过将其作为引用来调用 multiply 方法。
// 创建类 class Getsum { // 定义类的成员变量 num1: number = 145; num2: number = 243; // Method to get sum of two numbers doSum(): number { let sum = this.num1 + this.num2; return sum; } } class Multiplier { // 创建 Getsum 类的对象 object: Getsum = new Getsum(); // 在 Multiplier 类中调用 GetSum 类的 doSum() 方法 result = this.object.doSum(); // Method to multiply number multiply(): number { return this.result * 30; } } // 创建 Multiplier 类的对象 let multiplierObject: Multiplier = new Multiplier(); // 调用 multiplier 类的 multiply 方法 console.log( "Result we get after calling the multiply method is " + multiplierObject.multiply() );
编译后,它将生成以下 JavaScript 代码 -
// Creating the class var Getsum = /** @class */ (function () { function Getsum() { // 定义类的成员变量 this.num1 = 145; this.num2 = 243; } // Method to get sum of two numbers Getsum.prototype.doSum = function () { var sum = this.num1 + this.num2; return sum; }; return Getsum; }()); var Multiplier = /** @class */ (function () { function Multiplier() { // 创建 Getsum 类的对象 this.object = new Getsum(); // calling the doSum() method of the GetSum class inside the Multiplier class this.result = this.object.doSum(); } // Method to multiply number Multiplier.prototype.multiply = function () { return this.result * 30; }; return Multiplier; }()); // 创建 Multiplier 类的对象 var multiplierObject = new Multiplier(); // 调用 multiplier 类的 multiply 方法 console.log("调用 multiply 方法后得到的结果是 " + multiplierObject.multiply());
输出
上述代码将产生以下输出 −
调用 multiply 方法后得到的结果是 11640
在 TypeScript 中调用特定类的静态方法
我们可以直接访问静态方法,而无需引用声明该方法的类的对象。我们需要在方法定义前使用 static 关键字来定义静态方法。
语法
用户可以按照以下语法定义和调用静态方法。
class demoClass { static printMessage(): void { // 执行一些操作 } } demoClass.printMessage();
在上述语法中,我们定义了包含静态方法的类。此外,我们通过类名作为引用来调用该静态方法。
示例
在下面的示例中,我们创建了名为 demoClass 的类。我们通过在方法定义前插入 static 关键字,在类内部定义了静态方法,该方法用于打印消息。
最后,我们使用 demoClass 作为引用并调用了 printMessage() 方法。
class demoClass { // 在方法定义前添加 static 关键字,使其成为静态方法。 static printMessage(): void { console.log(" Static method name printMessage is called."); } } // 通过类名作为引用来调用静态方法 demoClass.printMessage();
编译后,它将生成以下 JavaScript 代码 -
var demoClass = /** @class */ (function () { function demoClass() { } // 在方法定义之前添加 static 关键字以使其成为静态的。 demoClass.printMessage = function () { console.log(" 调用静态方法名 printMessage。"); }; return demoClass; }()); // 以类名作为引用来调用静态方法 demoClass.printMessage();
输出
上述代码将产生以下输出 −
调用静态方法名 printMessage。
用户通过本教程学会了在 TypeScript 中调用类的方法。此外,他们还学会了调用特定类的静态方法。此外,用户还学会了将一个类的方法调用到另一个类中,这有助于他们重用代码。