
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境設定
- TypeScript - 基本語法
- TypeScript 與 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 與 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 中,靜態屬性屬於類本身,而不是類的例項。靜態方法和屬性可以在不使用類例項的情況下訪問。這意味著您不需要建立類的物件來使用這些方法和屬性。
TypeScript 中有兩種型別的屬性和方法。一種是例項屬性和方法,另一種是靜態屬性和方法。在這裡,您將學習有關靜態屬性和方法的知識。
靜態屬性和方法可用於建立實用程式函式和常量,這些函式和常量在不同例項之間不會發生變化。例如,如果您正在建立圓形類,並且想要在類中定義“PI”屬性,則可以將其設為靜態,因為它是一個常量。
靜態屬性
我們可以在屬性名稱之前使用“static”關鍵字來定義靜態屬性。
語法
您可以按照以下語法在 TypeScript 類中定義靜態屬性。
class className { static property_name: data_type = value; }
在上述語法中,我們在屬性名稱之前使用了“static”關鍵字來定義靜態屬性。
要訪問靜態屬性,我們可以使用類名後跟點號,再跟靜態屬性名稱,如下所示。
className.property_name;
示例
在下面的程式碼中,我們建立了“circle”類。該類包含“pi”靜態屬性,該屬性具有一個常數值。
class Circle { static pi: number = 3.14159; // Static property to hold the value of Pi } console.log("The value of the PI is: " + Circle.pi);
編譯後,它將生成以下 JavaScript 程式碼。
var Circle = /** @class */ (function () { function Circle() { } Circle.pi = 3.14159; // Static property to hold the value of Pi return Circle; }()); console.log("The value of the PI is: " + Circle.pi);
輸出
上述生成的 JavaScript 程式碼的輸出如下所示:
The value of the PI is: 3.14159
示例
在下面的程式碼中,我們定義了“student”類。它包含名為“studentCount”的靜態屬性,用於儲存學生的總數。
class Student { static studentCount: number = 0; // Static variable to store the count of students constructor(public name: string, public age: number) { this.name = name; this.age = age; Student.studentCount++; // Incrementing the count of students } // Method to display the student details display() { console.log(`Name: ${this.name}, Age: ${this.age}`); } } // Creating objects of Student class let student1 = new Student('John', 20); let student2 = new Student('Doe', 21); // Accessing static variable console.log("Total number of registered students is: " + Student.studentCount); // Output: 2
編譯後,它將生成以下 JavaScript 程式碼。
class Student { constructor(name, age) { this.name = name; this.age = age; this.name = name; this.age = age; Student.studentCount++; // Incrementing the count of students } // Method to display the student details display() { console.log(`Name: ${this.name}, Age: ${this.age}`); } } Student.studentCount = 0; // Static variable to store the count of students // Creating objects of Student class let student1 = new Student('John', 20); let student2 = new Student('Doe', 21); // Accessing static variable console.log("Total number of registered students is: " + Student.studentCount); // Output: 2
輸出
上述生成的 JavaScript 程式碼的輸出如下所示:
Total number of registered students is: 2
靜態方法
您可以在方法名稱之前使用“static”關鍵字來宣告靜態方法。
語法
您可以按照以下語法在 TypeScript 類中定義靜態方法。
class Class_name { static method_name(params) { // Code to be executed } }
在上述語法中,method_name 方法是一個靜態方法,它可以接受多個引數並返回值。
您可以透過使用類名訪問靜態方法來呼叫它,如下面的程式碼所示。
Class_name.method_name();
示例
在下面的程式碼中,“square”類包含“area()”靜態方法,該方法獲取正方形邊的值作為引數並返回正方形的面積。
class Square { // Define a static method static area(side: number) { return side * side; // return the area of the square } } // call the static method let area = Square.area(5); console.log(`Area of the square: ${area}`); // Output: Area of the square: 25
編譯後,它將生成以下 JavaScript 程式碼。
class Square { // Define a static method static area(side) { return side * side; // return the area of the square } } // call the static method let area = Square.area(5); console.log(`Area of the square: ${area}`); // Output: Area of the square: 25
輸出
上述生成的 JavaScript 程式碼的輸出如下所示:
Area of the square: 25
示例
下面的示例與本課中介紹的第二個示例非常相似。它有一個名為“studentCount”的私有靜態成員,用於儲存學生的總數。
class Student { private static studentCount: number = 0; // Static variable to store the count of students constructor(public name: string, public age: number) { this.name = name; this.age = age; Student.studentCount++; // Incrementing the count of students } // Method to get the count of students static getStudentCount() { return Student.studentCount; } } // Creating objects of Student class let student1 = new Student('John', 20); let student2 = new Student('Doe', 21); // Accessing static variable console.log("Total number of registered students is: " + Student.getStudentCount()); // Output: 2
編譯後,它將生成以下 JavaScript 程式碼。
class Student { constructor(name, age) { this.name = name; this.age = age; this.name = name; this.age = age; Student.studentCount++; // Incrementing the count of students } // Method to get the count of students static getStudentCount() { return Student.studentCount; } } Student.studentCount = 0; // Static variable to store the count of students // Creating objects of Student class let student1 = new Student('John', 20); let student2 = new Student('Doe', 21); // Accessing static variable console.log("Total number of registered students is: " + Student.getStudentCount()); // Output: 2
輸出
上述生成的 JavaScript 程式碼的輸出如下所示:
Total number of registered students is: 2
在即時開發中,靜態屬性可用於儲存應用程式版本、特定設定等值,因為它們保持不變。簡而言之,您可以使用靜態屬性來儲存常量值。此外,當方法的程式碼不依賴於任何例項屬性時,您可以使用靜態方法。