VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹
其實(shí) MSDN 的 VBScript 文檔中關(guān)于 Function 和 Sub 語句的部分提到過 Default 關(guān)鍵字:
Default
Used only with the Public keyword in a Class block to indicate that the Function procedure is the default method for the class. An error occurs if more than one Default procedure is specified in a class.
Default 只能在 Class 語句塊中與 Public 關(guān)鍵字一起使用來表明函數(shù)過程是類的默認(rèn)方法。如果類中一個(gè)以上的過程被定義為 Default,那么會出現(xiàn)錯誤。
一個(gè)簡單的例子:
Class MyClass
Public Default Function SayHello(name)
SayHello = "Hello, " & name
End Function
End Class
Set o = New MyClass
MsgBox o("demon")
很多面向?qū)ο蟮恼Z言都能使用構(gòu)造函數(shù)來初始化類的對象,但是 VBS 卻沒有構(gòu)造函數(shù)的概念,只是提供了一個(gè)類初始化事件來初始化對象:
Class TestClass
' Setup Initialize event.
Private Sub Class_Initialize
MsgBox("TestClass started")
End Sub
' Setup Terminate event.
Private Sub Class_Terminate
MsgBox("TestClass terminated")
End Sub
End Class
' Create an instance of TestClass.
Set X = New TestClass
' Destroy the instance.
Set X = Nothing
雖然看起來很像構(gòu)造函數(shù),但是卻不能帶參數(shù),沒有辦法像其他語言那樣用特定的參數(shù)來初始化對象。
有了 Default 關(guān)鍵字之后,我們可以模擬實(shí)現(xiàn)構(gòu)造函數(shù)的功能:
'Author: Demon
'Date: 2011/09/29
'Website: http://demon.tw
Class Rectangle
Private height, width
Public Default Function Construtor(h, w)
height = h : width = w
Set Construtor = Me
End Function
Public Property Get Area
Area = height * width
End Property
End Class
'看起來是不是很像構(gòu)造函數(shù)呢
Set r = (New Rectangle)(6, 8)
MsgBox r.Area
參考鏈接:VBScript's default keyword
原文:http://demon.tw/programming/vbs-default-keyword.html
欄 目:vb
下一篇:提權(quán)vbs代碼
本文標(biāo)題:VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹
本文地址:http://www.jygsgssxh.com/a1/vb/7415.html
您可能感興趣的文章
- 01-10下載文件到本地運(yùn)行的vbs
- 01-10VBS中的正則表達(dá)式的用法大全 <font color=red>原創(chuàng)&
- 01-10VBS中SendKeys的基本應(yīng)用
- 01-10VBScript教程 第十一課深入VBScript
- 01-10用VBSCRIPT控制ONSUBMIT事件
- 01-10VBScript語法速查及實(shí)例說明
- 01-10VBS中Select CASE的其它用法
- 01-10VBScript教程 第七課使用條件語句
- 01-10vbscript 可以按引用傳遞參數(shù)嗎?
- 01-10VBScript教程 第二課在HTML頁面中添加VBscript代碼


閱讀排行
本欄相關(guān)
- 01-10下載文件到本地運(yùn)行的vbs
- 01-10飄葉千夫指源代碼,又稱qq刷屏器
- 01-10SendKeys參考文檔
- 01-10什么是一個(gè)高效的軟件
- 01-10VBS中的正則表達(dá)式的用法大全 &l
- 01-10exe2swf 工具(Adodb.Stream版)
- 01-10VBS中SendKeys的基本應(yīng)用
- 01-10用VBSCRIPT控制ONSUBMIT事件
- 01-10VBScript教程 第十一課深入VBScript
- 01-10VBScript語法速查及實(shí)例說明
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什


