今天就跟大家聊聊有關(guān)VBS中如何使用Default關(guān)鍵字,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

其實(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,那么會(huì)出現(xiàn)錯(cuò)誤。
一個(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ù)的功能:
復(fù)制代碼 代碼如下:
'Author: Demon
'Date: 2011/09/29
'Website: /tupian/20230522/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
看完上述內(nèi)容,你們對VBS中如何使用Default關(guān)鍵字有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
標(biāo)題名稱:VBS中如何使用Default關(guān)鍵字-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.chinadenli.net/article10/igddo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、企業(yè)建站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容