
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境設定
- TypeScript - 基本語法
- TypeScript vs. JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - Any
- TypeScript - Never
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - 符號
- TypeScript - null vs. undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策
- TypeScript - If 語句
- TypeScript - If Else 語句
- TypeScript - 巢狀 If 語句
- TypeScript - Switch 語句
- TypeScript - 迴圈
- TypeScript - For 迴圈
- TypeScript - While 迴圈
- TypeScript - Do While 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - 函式構造器
- TypeScript - Rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 擴充套件介面
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 存取器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - Keyof 型別運算子
- TypeScript - Typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 其他
- TypeScript - 三斜槓指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 實用程式型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - 存取器
TypeScript 中的存取器提供了一種使用 getter 和 setter 方法訪問和設定類成員值的方式。它們控制如何訪問類成員以讀取或寫入其值。
存取器對於在 TypeScript 中實現封裝很有用,封裝限制了對類成員的訪問,僅限於類的成員函式,防止第三方未經授權的訪問。
TypeScript 支援以下方法來訪問和更改類成員
getter
setter
TypeScript 中的 Getter
Getter 用於訪問類成員的值並管理如何在類外部訪問這些值。它們可以使用 'get' 關鍵字建立。
語法
您可以按照以下語法在 TypeScript 中使用 getter。
class class_name { // Define private variable here. // Defining the getter get getter_name(): return_type { // Return variable value } } let val = class_name.getter_name;
在上述語法中,method_name 方法是一個靜態方法,可以接受多個引數並返回值。
要使用 getter 訪問值,我們可以使用類名後跟一個點,然後是 getter 方法名。
示例
在下面的示例中,我們定義了 Person 類,其中包含 'Name' 私有變數。它還包含 constructor() 方法,該方法初始化 'Name' 變數的值。
// Defining the Person class class Person { // Defining the private field private Name: string; // Defining the constructor constructor(Name: string) { this.Name = Name; } // Defining the getter get SName(): string { return this.Name; } } // Creating an instance of the Person class const person = new Person("John"); console.log(person.SName); // Outputs: John
編譯後,它將生成以下 JavaScript 程式碼。
// Defining the Person class class Person { // Defining the constructor constructor(Name) { this.Name = Name; } // Defining the getter get SName() { return this.Name; } } // Creating an instance of the Person class const person = new Person("John"); console.log(person.SName); // Outputs: John
輸出
John
示例
在下面的程式碼中,Temperature 類包含 'celsius' 私有變數。constructor() 方法初始化 'celsius' 變數的值。
// Define a class Temperature with a property Celsius of type number. class Temperature { private celsius: number; // Define a constructor that initializes the Celsius property. constructor(celsius: number) { this.celsius = celsius; } // Define a getter fahrenheit that returns the temperature in Fahrenheit. get fahrenheit(): number { return (this.celsius * 9 / 5) + 32; } } // Create an instance of the Temperature class with a temperature of 25 degrees Celsius. const temp = new Temperature(25); console.log("The Fahrenheit value is: " + temp.fahrenheit); // Outputs: 77
編譯後,它將生成以下 JavaScript 程式碼。
// Define a class Temperature with a property Celsius of type number. class Temperature { // Define a constructor that initializes the Celsius property. constructor(celsius) { this.celsius = celsius; } // Define a getter fahrenheit that returns the temperature in Fahrenheit. get fahrenheit() { return (this.celsius * 9 / 5) + 32; } } // Create an instance of the Temperature class with a temperature of 25 degrees Celsius. const temp = new Temperature(25); console.log("The Fahrenheit value is: " + temp.fahrenheit); // Outputs: 77
輸出
The Fahrenheit value is: 77
TypeScript 中的 Setter
在 TypeScript 中,setter 用於設定類成員的值,而無需在類外部訪問它們。它們使用 'set' 關鍵字來定義 setter 方法。
語法
您可以按照以下語法在 TypeScript 中使用 setter。
class class_name { // Define private variable here. // Defining the setter set setter_name(val: type) { // Set variable value } } class_name.setter_name = val;
在上述語法中,我們使用了 'set' 關鍵字後跟 'setter_name' 來定義一個 setter。它只接受一個值作為引數,並在 setter 方法內部使用它來更改任何私有類變數的值。
要使用 setter 方法,您需要使用類名後跟一個點,然後是 setter 方法名,併為其賦值。
示例
在下面的程式碼中,我們定義了 TextContainer 類,其中包含 '_content' 私有變數來儲存文字。
// Define a class with a private property and a getter/setter method class TextContainer { // Define a private property private _content: string = ''; // Setter method set content(value: string) { this._content = value.trim().toLowerCase(); } // Getter method get content(): string { return this._content; } } // Create an instance of the class and set the content const text = new TextContainer(); text.content = " Hello, WORLD! "; console.log(text.content); // Outputs: hello, world!
編譯後,它將生成以下 JavaScript 程式碼。
// Define a class with a private property and a getter/setter method class TextContainer { constructor() { // Define a private property this._content = ''; } // Setter method set content(value) { this._content = value.trim().toLowerCase(); } // Getter method get content() { return this._content; } } // Create an instance of the class and set the content const text = new TextContainer(); text.content = " Hello, WORLD! "; console.log(text.content); // Outputs: hello, world!
輸出
hello, world!
在 TypeScript 中使用存取器來實現封裝非常重要。您還可以在單個類中建立多個 getter 和 setter 方法。