VBScript作為腳本語言不僅能夠編寫簡單的腳本,而且還能夠創建及使用對象編寫復雜的腳本,如Class對象,數據字典,操作文件夾及文件,錯誤處理,正則表達式等等。
1. Class對象
2. Dictionary對象
3. FileSystemObject對象
4. Err對象
5. RegExp對象
1. Class對象
使用Class語句可以創建一個對象,可以為它編寫字段、屬性及方法,它只有兩個對象事件——Initialize與Terminate。首先來看一個簡單的Class示例:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Class User '私有字段,也可以使用Public語句定義公有字段 Private m_UserName Private m_Profile 'Initialize事件相當于構造函數 Private Sub Class_Initialize m_UserName = Empty '設置UserName初始值為空字符串 End Sub 'Terminate事件相當于析構函數 Private Sub Class_Terminate Set m_Profile = Nothing '將對象設置為Nothing,銷毀對象 End Sub 'Property Get語句,獲取屬性值或對象引用,Default只與Public一起使用,表示該屬性為類的默認屬性 Public Default Property Get UserName UserName = m_UserName End Property 'Property Let語句,設置屬性值 Public Property Let UserName(newUserName) m_UserName = newUserName End Property Public Property Get Profile Set Profile = m_Profile End Property 'Property Set語句,設置屬性對象引用 Public Property Set Profile(newProfile) Set m_Profile = newProfile End Property 'ToString方法 Public Function ToString() ToString = "Hello! " & Me .UserName 'Me相當于C#中的this關鍵字 End Function End Class |
原文轉自:http://www.anti-gravitydesign.com