如何在 TypeScript 中使用 getter 和 setter?

typescriptserver side programmingprogramming

在 TypeScript 中,gettersetter 是两个术语,分别用于获取和设置类成员的值。但是,用户可以通过点运算符直接访问类的 公共成员,方法是将特定类的对象作为引用。要访问类的 私有成员,我们只需使用 getter 方法即可。在本教程中,我们将学习如何在 TypeScript 中使用 getter 和 setter。

使用 getter 访问类的私有成员

就像我们在 C++ 和 Java 等其他编程语言中创建方法来访问类的私有成员一样,在 TypeScript 中也可以使用 getter 来实现同样的目的。我们可以在访问器方法的定义前写入 get 关键字来创建 getter,该方法返回某个值。

语法

用户可以按照以下语法定义 getter,并使用它来获取私有成员的值。

class employee {
   private emp_Name: string = "Shubham Vora";
   public get name() {
      return this.emp_Name;
   }
}

let man = new employee();
let name_Value = man.name;

在上述语法中,我们使用 get 关键字定义了 name() 方法,并使用 name 方法访问类的私有成员 emp_Name。

在 TypeScript 中使用"getters"的步骤

步骤 1 - 创建员工类。

步骤 2 - 另外,将 emp_Name、age_of_emp 和 role 添加为员工类的私有成员。不要忘记定义每个变量的类型并用某个值初始化它。

步骤 3 − 接下来,使用 get 关键字并定义 getter 方法以访问员工的姓名和年龄。

步骤 4 − 现在,创建员工类的对象。

步骤 5 − 使用员工类的对象作为引用,并使用点运算符调用 getter 方法。

开发人员可以观察到,我们没有像普通方法那样调用 getter 方法,并且在调用它时没有写括号。

示例

在下面的示例中,我们使用 getter 方法访问员工类的私有成员。在输出中,用户可以观察到我们正在使用 getter 访问姓名和年龄并显示它们。

class employee {
  // 创建私有类成员
  private emp_Name: string = "Shubham Vora";
  private age_of_emp: number = 22;
  private role: string = "Content Writer";
  // 获取员工姓名的 getter
  public get name() {
    return this.emp_Name;
  }
  // 获取年龄的 getter
   public get age() {
      return this.age_of_emp;
   }
}

// 创建 Student 类的对象
let man = new employee();

// 调用 getter 函数(无需括号)
let name_Value = man.name;
// 打印姓名
console.log("The name of the employee is " + name_Value);
console.log("The age of the employee is " + man.age);

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

var employee = /** @class */ (function () {
   function employee() {
      // 创建私有类成员
      this.emp_Name = "Shubham Vora";
      this.age_of_emp = 22;
      this.role = "Content Writer";
   }
   Object.defineProperty(employee.prototype, "name", {
      // 获取员工姓名
      get: function () {
         return this.emp_Name;
      },
      enumerable: true,
      configurable: true
   });
   Object.defineProperty(employee.prototype, "age",{
      // 获取年龄的 getter
      get: function () {
         return this.age_of_emp;
      },
      enumerable: true,
      configurable: true
   });
   return employee;
}());
// 创建 Student 类的对象
var man = new employee();
// 调用 getter 函数(无需括号)
var name_Value = man.name;
// 打印姓名
console.log("员工姓名为 " + name_Value);
console.log("员工年龄为 " + man.age);

输出

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

员工姓名为 Shubham Vora
员工年龄为 22

在 TypeScript 中使用 setter 函数设置类成员的值

通常,在 TypeScript 中,我们无法通过将对象作为引用来更改类的私有成员的值。因此,我们需要使用 setter 函数。设置器的工作方式与普通方法相同,但我们需要在方法定义前添加"set"关键字,才能使其成为设置器。

语法

用户可以按照以下语法使用设置器来更改类的私有成员的值。

class employee {
   private emp_Name: string = "Shubham Vora";
   public set name(new_value) {
      this.emp_Name = new_value;
   }
}

let man = new employee();
let man.name = "Shubham";

在上述语法中,我们使用 set 关键字创建了 setters 方法。通过使用 setters,我们可以更改 emp_name 变量的值。

参数

setters 方法只接受一个参数,具体说明如下。

  • new_Value − 它是一个值,我们需要将其设置为私有成员。

在 TypeScript 中使用 setters 的步骤

步骤 1 − 使用 set 关键字创建 setters,以更改 emp_Name 变量的值。

步骤 2 − 将 new_Value 作为 setters 方法的参数传递。

步骤 3 − 创建 employee 类的对象。我们将通过调用 setter 方法来更改 emp_Name 的值。

步骤 4 - 打印 emp_Name 变量的更新值。

示例

在下面的示例中,我们使用了 employee 类的对象作为引用,并使用 setter 方法的名称来访问该方法。我们将新值赋给 setter,就像我们将值赋给类变量一样。调用 setter 时,它表示用户无需传递新值作为参数,而是可以使用赋值运算符并将新值作为右操作数。

class employee {
  // 创建私有类成员
  private emp_Name: string = "Shubham Vora";
  private age_of_emp: number = 22;
  private role: string = "Content Writer";
  // 获取员工姓名的 getter
   public get name() {
      return this.emp_Name;
   }
   public set name(new_value: string) {
      this.emp_Name = new_value;
   }
}

// 创建 Student 类的对象
let man = new employee();

// 调用 getter 函数(无需括号)
let name_Value = man.name;
// 打印姓名
console.log("员工姓名为 " + name_Value);
// 使用 setter 函数更新员工姓名
man.name = "Jems Bond";
console.log(
   "The name of the employee after updating it using the setters is " + man.name
);

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

var employee = /** @class */ (function () {
   function employee() {
      // 创建私有类成员
      this.emp_Name = "Shubham Vora";
      this.age_of_emp = 22;
      this.role = "Content Writer";
   }
   Object.defineProperty(employee.prototype, "name", {
      // 获取员工姓名
      get: function () {
         return this.emp_Name;
      },
      set: function (new_value) {
         this.emp_Name = new_value;
      },
      enumerable: true,
      configurable: true
   });
   return employee;
}());
// 创建 Student 类的对象
var man = new employee();
// 调用 getter 函数(无需括号)
var name_Value = man.name;
// 打印姓名
console.log("员工姓名为 " + name_Value);
// 使用 setter 函数更新员工姓名
man.name = "Jems Bond";
console.log("使用 setter 函数更新后,员工姓名为 " + man.name);

输出

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

员工姓名为 Shubham Vora
使用 setter 函数更新后,员工姓名为 Jems Bond

在本教程中,我们学习了如何在 TypeScript 中使用 getter 和 setter 函数。


相关文章