如何在 TypeScript 中调用特定类的的方法?


在本教程中,用户将学习如何在 TypeScript 中调用特定类的的方法。类是面向对象编程的基本概念。简单来说,它包含成员变量和方法,我们可以通过创建该特定类的对象来访问它们。因此,类是我们为该特定类创建的对象的蓝图。

类可以在 TypeScript 中包含函数,我们也可以称它们为方法。因此,用户需要学习如何访问和调用特定类的的方法。

在 TypeScript 中调用特定类的的方法

在 TypeScript 中,我们可以创建特定类的对象实例。用户可以使用点运算符通过以该类的对象为参考来访问特定类的公共成员和方法。

语法

用户可以按照以下语法学习如何创建类的对象,并使用对象和点运算符访问该类的的方法。

// Creating the class
class Getsum {
   doSum(multiplier: number): number {
	   // perform some operation
      return value;
   }
}
var object: Getsum = new Getsum();
let result = object.doSum(10);

在上面的语法中,我们只是创建了包含方法的类。此外,我们还定义了类的对象实例,并使用点运算符访问了 doSum() 方法。

示例

在下面的示例中,我们创建了 'GetSum' 类。我们还定义了类型为数字的 num1 和 num2 作为类成员,并分别用 10 和 20 值初始化它们。

接下来,我们定义了 doSum() 方法,该方法将 'multiplier' 作为参数并返回数字值。在方法内部,我们使用 'this' 关键字访问 num1 和 num2,并将两者都乘以 multiplier。之后,我们对 num1 和 num2 的新值进行求和,将其存储在 'sum' 变量中并返回它。

Open Compiler
// Creating the class class Getsum { // defining the member variables of the class num1: number = 10; num2: number = 20; // Method to get sum of two numbers doSum(multiplier: number): number { // Multiply num1 and num2 by multiplier 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 let sum = this.num1 + this.num2; return sum; } } // Creating the object of the Getsum class var object: Getsum = new Getsum(); // calling the doSum() method let result = object.doSum(10); console.log("Result we get after calling the doSum method is " + result);

编译后,它将生成以下 JavaScript 代码:

Open Compiler
// Creating the class var Getsum = /** @class */ (function () { function Getsum() { // defining the member variables of the class this.num1 = 10; this.num2 = 20; } // Method to get sum of two numbers Getsum.prototype.doSum = function (multiplier) { // Multiply num1 and num2 by multiplier 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; }()); // Creating the object of the Getsum class var object = new Getsum(); // calling the doSum() method var result = object.doSum(10); console.log("Result we get after calling the doSum method is " + result);

输出

以上代码将产生以下输出:

Result we get after calling the doSum method is 300

从一个类调用另一个类的的方法

在 TypeScript 中,我们可以访问一个类的公共方法到另一个类,并可以调用它。有时,需要将其他类的少量方法调用到特定类。

语法

用户可以按照以下语法从一个类调用另一个类的的方法。

class Getsum {
   doSum(): number {
      // perform some operation
   }
}

class Multiplier {
   object: Getsum = new Getsum();
   result = this.object.doSum();
   multiply(): number {
      // Perform some operation
   }
}
let multiplierObject: Multiplier = new Multiplier();
let result =  multiplierObject.multiply()

在上面的语法中,我们创建了两个不同的类。我们已经将第一个类的的方法调用到第二个类,并在第二个类的另一个方法中使用了它的返回值。

示例

在下面的示例中,GetSum 类包含 doSum() 方法作为成员,该方法返回数字值。在 Multiplier 类中,我们创建了 GetSum 类的对象并通过以对象为参考调用了 GetSum 类的 doSum() 方法,并将它的返回值存储在 result 中。

此外,我们在 Multiplier 类中创建了 multiply 方法,该方法将 result 乘以 30 并返回乘积值。最后,我们创建了 Multiplier 类的对象并通过以它为参考调用了 multiply 方法。

Open Compiler
// Creating the class class Getsum { // defining the member variables of the class 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 { // Creating the object of the Getsum class object: Getsum = new Getsum(); // calling the doSum() method of the GetSum class inside the Multiplier class result = this.object.doSum(); // Method to multiply number multiply(): number { return this.result * 30; } } // Creating the object of the Multiplier class let multiplierObject: Multiplier = new Multiplier(); // Invoking the multiply method of multiplier class console.log( "Result we get after calling the multiply method is " + multiplierObject.multiply() );

编译后,它将生成以下 JavaScript 代码:

Open Compiler
// Creating the class var Getsum = /** @class */ (function () { function Getsum() { // defining the member variables of the class 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() { // Creating the object of the Getsum class 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; }()); // Creating the object of the Multiplier class var multiplierObject = new Multiplier(); // Invoking the multiply method of multiplier class console.log("Result we get after calling the multiply method is " + multiplierObject.multiply());

输出

以上代码将产生以下输出:

Result we get after calling the multiply method is 11640

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

在 TypeScript 中调用特定类的静态方法

我们可以直接访问静态方法,而无需获取声明该方法的类的对象的引用。我们需要在方法定义之前使用 static 关键字来定义静态方法。

语法

用户可以按照以下语法定义和调用静态方法。

class demoClass {
   static printMessage(): void {
      // perform some operation
   }
}
demoClass.printMessage();

在上面的语法中,我们定义了包含静态方法的类。此外,我们还通过以类名为参考来调用了静态方法。

示例

在下面的示例中,我们创建了名为 demoClass 的类。我们通过在方法定义之前插入 static 关键字在类中定义了静态方法,该方法打印消息。

最后,我们使用 demoClass 作为参考并调用了 printMessage() 方法。

Open Compiler
class demoClass { // add static keyword before method defination to make it static. static printMessage(): void { console.log(" Static method name printMessage is called."); } } // call the static method by taking class name as a reference demoClass.printMessage();

编译后,它将生成以下 JavaScript 代码:

Open Compiler
var demoClass = /** @class */ (function () { function demoClass() { } // add static keyword before method defination to make it static. demoClass.printMessage = function () { console.log(" Static method name printMessage is called."); }; return demoClass; }()); // call the static method by taking class name as a reference demoClass.printMessage();

输出

以上代码将产生以下输出:

Static method name printMessage is called.

用户通过本教程学习了如何在 TypeScript 中调用类的的方法。此外,他们还学习了如何调用特定类的的静态方法。此外,用户还学习了如何将一个类的的方法调用到另一个类,这有助于他们重用代码。

更新于: 2022年12月16日

6K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告