VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹
更新時(shí)間:2011年10月26日 21:59:54 作者:
很多人并不知道 VBS 中還有個(gè) Default 關(guān)鍵字,更不用說(shuō)知道 Default 關(guān)鍵字的用法。
其實(shí) MSDN 的 VBScript 文檔中關(guān)于 Function 和 Sub 語(yǔ)句的部分提到過 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 語(yǔ)句塊中與 Public 關(guān)鍵字一起使用來(lái)表明函數(shù)過程是類的默認(rèn)方法。如果類中一個(gè)以上的過程被定義為 Default,那么會(huì)出現(xiàn)錯(cuò)誤。
一個(gè)簡(jiǎn)單的例子:
Class MyClass
Public Default Function SayHello(name)
SayHello = "Hello, " & name
End Function
End Class
Set o = New MyClass
MsgBox o("demon")
很多面向?qū)ο蟮恼Z(yǔ)言都能使用構(gòu)造函數(shù)來(lái)初始化類的對(duì)象,但是 VBS 卻沒有構(gòu)造函數(shù)的概念,只是提供了一個(gè)類初始化事件來(lái)初始化對(duì)象:
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
雖然看起來(lái)很像構(gòu)造函數(shù),但是卻不能帶參數(shù),沒有辦法像其他語(yǔ)言那樣用特定的參數(shù)來(lái)初始化對(duì)象。
有了 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
'看起來(lái)是不是很像構(gòu)造函數(shù)呢
Set r = (New Rectangle)(6, 8)
MsgBox r.Area
復(fù)制代碼 代碼如下:
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 語(yǔ)句塊中與 Public 關(guān)鍵字一起使用來(lái)表明函數(shù)過程是類的默認(rèn)方法。如果類中一個(gè)以上的過程被定義為 Default,那么會(huì)出現(xiàn)錯(cuò)誤。
一個(gè)簡(jiǎn)單的例子:
復(fù)制代碼 代碼如下:
Class MyClass
Public Default Function SayHello(name)
SayHello = "Hello, " & name
End Function
End Class
Set o = New MyClass
MsgBox o("demon")
很多面向?qū)ο蟮恼Z(yǔ)言都能使用構(gòu)造函數(shù)來(lái)初始化類的對(duì)象,但是 VBS 卻沒有構(gòu)造函數(shù)的概念,只是提供了一個(gè)類初始化事件來(lái)初始化對(duì)象:
復(fù)制代碼 代碼如下:
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
雖然看起來(lái)很像構(gòu)造函數(shù),但是卻不能帶參數(shù),沒有辦法像其他語(yǔ)言那樣用特定的參數(shù)來(lái)初始化對(duì)象。
有了 Default 關(guān)鍵字之后,我們可以模擬實(shí)現(xiàn)構(gòu)造函數(shù)的功能:
復(fù)制代碼 代碼如下:
'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
'看起來(lái)是不是很像構(gòu)造函數(shù)呢
Set r = (New Rectangle)(6, 8)
MsgBox r.Area
參考鏈接:VBScript's default keyword
原文:http://demon.tw/programming/vbs-default-keyword.html
相關(guān)文章
vbs 調(diào)用中文語(yǔ)音讓你電腦聽你的命令的實(shí)現(xiàn)代碼
關(guān)于vbs調(diào)用中文語(yǔ)音讓你電腦聽你的命令的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-10-10
VBS 十六進(jìn)制異或加密實(shí)現(xiàn)代碼
文件不要過大,最好不要超過 100 KB,密鑰不要用數(shù)字,不想改了。理論上支持任何二進(jìn)制文件格式2013-07-07
vbs中關(guān)于計(jì)算機(jī)名的相關(guān)討論
vbs中關(guān)于計(jì)算機(jī)名的相關(guān)討論...2007-03-03
用vbs實(shí)現(xiàn)獲取電腦硬件信息的腳本_最新版
比較迅速的獲取硬件信息排序后的txt文件把后綴名改為csv就是表格了,精簡(jiǎn)、整理后輸出打印就OK了。 如此詳細(xì)的信息,給老板看,一定可以讓老板對(duì)你另眼相看。 即使自己看,也能發(fā)現(xiàn)很多料想不到的的信息。2008-05-05
在vbscript中判斷COM類是否存在的實(shí)現(xiàn)代碼
在VBS中,我們有時(shí)需要判斷一個(gè)COM類是否存在,以便執(zhí)行不同的操作,需要的朋友可以參考下2018-06-06
VBS 強(qiáng)制關(guān)閉Symantec Endpoint Protection的代碼
很多企業(yè)電腦系統(tǒng)是Windows Xp,使用Windows server 2003 來(lái)控制,其中客戶端得殺毒軟件有不少是使用 Symantec Endpoint Protection2013-01-01

