VBS中如何使用Default關(guān)鍵字-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站為企業(yè)級客戶提高一站式互聯(lián)網(wǎng)+設(shè)計服務(wù),主要包括網(wǎng)站制作、成都做網(wǎng)站、app軟件開發(fā)公司、微信小程序定制開發(fā)、宣傳片制作、LOGO設(shè)計等,幫助客戶快速提升營銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗豐富的經(jīng)驗,可以確保每一個作品的質(zhì)量和創(chuàng)作周期,同時每年都有很多新員工加入,為我們帶來大量新的創(chuàng)意。 

其實 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)方法。如果類中一個以上的過程被定義為 Default,那么會出現(xiàn)錯誤。
一個簡單的例子:


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ù)的概念,只是提供了一個類初始化事件來初始化對象:


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)鍵字之后,我們可以模擬實現(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)鍵字有進一步的了解嗎?如果還想了解更多知識或者相關(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://bm7419.com/article10/igddo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、手機網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計、移動網(wǎng)站建設(shè)、企業(yè)建站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營