濮阳杆衣贸易有限公司

主頁 > 知識庫 > VBS類構造函數(shù)與Default關鍵字使用介紹

VBS類構造函數(shù)與Default關鍵字使用介紹

熱門標簽:新密防封卡外呼系統(tǒng)違法嗎 前鋒辦理400電話申請 外呼營銷下單系統(tǒng) 辦理膠州400電話財稅 陜西高頻外呼回撥系統(tǒng)哪家好 打電話的外呼系統(tǒng)貴不貴 百度地圖標注怎么卸載 海外美發(fā)店地圖標注 加盟電銷機器人好的品牌
其實 MSDN 的 VBScript 文檔中關于 Function 和 Sub 語句的部分提到過 Default 關鍵字:
復制代碼 代碼如下:

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 關鍵字一起使用來表明函數(shù)過程是類的默認方法。如果類中一個以上的過程被定義為 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言都能使用構造函數(shù)來初始化類的對象,但是 VBS 卻沒有構造函數(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

雖然看起來很像構造函數(shù),但是卻不能帶參數(shù),沒有辦法像其他語言那樣用特定的參數(shù)來初始化對象。
有了 Default 關鍵字之后,我們可以模擬實現(xiàn)構造函數(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
'看起來是不是很像構造函數(shù)呢
Set r = (New Rectangle)(6, 8)
MsgBox r.Area

參考鏈接:VBScript's default keyword
原文:http://demon.tw/programming/vbs-default-keyword.html

標簽:阜陽 四平 河南 伊春 梅州 咸陽 牡丹江 武威

巨人網(wǎng)絡通訊聲明:本文標題《VBS類構造函數(shù)與Default關鍵字使用介紹》,本文關鍵詞  VBS,類,構造,函數(shù),與,Default,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《VBS類構造函數(shù)與Default關鍵字使用介紹》相關的同類信息!
  • 本頁收集關于VBS類構造函數(shù)與Default關鍵字使用介紹的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    浦县| 滕州市| 阿巴嘎旗| 安乡县| 鲁山县| 仁怀市| 色达县| 庆安县| 城市| 两当县| 子洲县| 耒阳市| 辽阳市| 凤山县| 皋兰县| 蒙城县| 普兰县| 定兴县| 定安县| 广安市| 金秀| 射阳县| 仙桃市| 营山县| 潮安县| 缙云县| 镇宁| 横山县| 百色市| 南平市| 云和县| 长治市| 西和县| 涟源市| 宁海县| 尖扎县| 寿阳县| 滕州市| 荥阳市| 日照市| 龙海市|