TypeScript get指南

742 阅读4分钟

TypeScript get

TypeScript get简介

每当我们需要访问TypeScript中任何对象的属性时,我们都会利用一种叫做get方法的方法,利用它可以提取变量的值。get方法在程序中使用关键字get,get方法可以是公开的、私有的或保护的。程序中使用的get方法可以使用定义get方法的类的名称来调用,也可以直接调用get方法。要在程序中使用get方法,编译器应设置为EMCAScript 5或更高,低于EMCAScript 5则不支持。

在TypeScript中声明get方法的语法。

get name_of_the_method(){ //block of code }

其中name_of_the_method代表使用get关键字定义的方法名称。

在TypeScript中get方法的工作

  • get方法可以在程序中定义,以提取任何变量的值或访问TypeScript中任何对象的属性。
  • get关键字在程序中与方法的名称一起使用,在该方法中要执行的代码写在对象上.methodname()。
  • get方法可以是公开的、私有的或保护的,程序中使用的get方法可以使用定义get方法的类的名称来调用,也可以直接调用get方法。
  • 通过使用get方法,我们可以实现对程序中如何访问一个对象的控制。

TypeScript get的例子

下面给出了TypeScript get的例子。

例子#1

TypeScript程序演示了get方法的使用,以获得一个给定数字的阶乘,并在屏幕上显示其输出。

代码。

class check { //a private variable of type number is defined and stored in value private value:number = 10; //a private variable of type number is defined and stored in u private u:number; //a private variable of type number is defined and stored in fact private fact = 1; //for loop is defined to compute the factorial by decrement the given value stepwise by 1 until it is greater than or equal to 1, inside the method factorial by using the keyword get get factorial() { for(this.u = this.value;this.u>=1;this.u--) { this.fact = this.fact * this.u; } return this.fact; } } //The factorial of the given value is displayed as the output on the screen console.log("The factorial of the given value is:\n") //an instance of the class check is created within which the get method is defined to display the output on the screen console.log(new check().factorial);

输出。

TypeScript get 1

在上面的程序中,定义了一个数字类型的私有变量并存储在值中。然后定义了另一个数字类型的私有变量,并存储在u中。然后定义一个for循环,通过使用关键字get,在方法factorial中逐步递减给定值,直到大于或等于1,来计算阶乘。然后创建一个类的实例,在该实例中定义get方法以在屏幕上显示输出。输出结果显示在上面的快照中。

示例#2

TypeScript程序演示了get方法的使用,以找到一个数字的幂,并在屏幕上显示它的输出。

代码。

class check { //a private variable of type number is defined and stored in value private value:number = 10; //a private variable of type number is defined and stored in u private u:number = 2; //get keyword is used to define the method power to obtain the power of a given number get power() { return Math.pow(this.value, this.u); } } //The power of the given value is displayed as the output on the screen console.log("The power of the given value is:\n") //an instance of the class check is created within which the get method is defined to display the output on the screen console.log(new check().power);

输出。

power of a number

在上面的程序中,定义了一个数字类型的私有变量并存储在值中。然后定义了另一个数字类型的私有变量并存储在u中。然后通过使用get关键字,定义了power方法来计算一个给定数字的幂。然后创建一个类的实例,在该实例中定义了get方法来在屏幕上显示输出。输出结果显示在上面的快照中。

例子#3

TypeScript程序演示了get方法的使用,以找到一个数字的平方根,并在屏幕上显示它的输出。

代码。

class check { //a private variable of type number is defined and stored in value private value:number = 4; //get keyword is used to define the method squareroot to find the square root of a given number get squareroot() { return Math.sqrt(this.value); } } //The square root of the given value is displayed as the output on the screen console.log("The square root of the given value is:\n") //an instance of the class check is created within which the get method is defined to display the output on the screen console.log(new check().squareroot);

输出。

TypeScript get 3

在上面的程序中,定义了一个数字类型的私有变量并存储在value中。然后通过使用get关键字,定义了squareeroot方法来计算一个给定数字的平方根。然后创建了一个类的实例,在该实例中定义了get方法来在屏幕上显示输出。输出结果显示在上面的快照中。

推荐文章

这是一个关于TypeScript get的指南。在这里,我们分别讨论了TypeScript中get方法的介绍、工作和例子。你也可以看看下面的文章来了解更多------。

  1. TypeScript数组
  2. TypeScript类型
  3. TypeScript版本
  4. TypeScript操作符

The postTypeScript getappeared first onEDUCBA.