午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹
    來源:易賢網(wǎng) 閱讀:1218 次 日期:2016-07-05 14:51:34
    溫馨提示:易賢網(wǎng)小編為您整理了“VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹”,方便廣大網(wǎng)友查閱!

    很多人并不知道 VBS 中還有個 Default 關(guān)鍵字,更不用說知道 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)方法。如果類中一個以上的過程被定義為 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)鍵字之后,我們可以模擬實(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

    更多信息請查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)