校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

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

VBS類構造函數與Default關鍵字使用介紹

熱門標簽:新密防封卡外呼系統違法嗎 前鋒辦理400電話申請 外呼營銷下單系統 辦理膠州400電話財稅 陜西高頻外呼回撥系統哪家好 打電話的外呼系統貴不貴 百度地圖標注怎么卸載 海外美發店地圖標注 加盟電銷機器人好的品牌
其實 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 關鍵字一起使用來表明函數過程是類的默認方法。如果類中一個以上的過程被定義為 Default,那么會出現錯誤。
一個簡單的例子:
復制代碼 代碼如下:

Class MyClass
Public Default Function SayHello(name)
SayHello = "Hello, " name
End Function
End Class
Set o = New MyClass
MsgBox o("demon")

很多面向對象的語言都能使用構造函數來初始化類的對象,但是 VBS 卻沒有構造函數的概念,只是提供了一個類初始化事件來初始化對象:
復制代碼 代碼如下:

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

雖然看起來很像構造函數,但是卻不能帶參數,沒有辦法像其他語言那樣用特定的參數來初始化對象。
有了 Default 關鍵字之后,我們可以模擬實現構造函數的功能:
復制代碼 代碼如下:

'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
'看起來是不是很像構造函數呢
Set r = (New Rectangle)(6, 8)
MsgBox r.Area

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

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

巨人網絡通訊聲明:本文標題《VBS類構造函數與Default關鍵字使用介紹》,本文關鍵詞  VBS,類,構造,函數,與,Default,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《VBS類構造函數與Default關鍵字使用介紹》相關的同類信息!
  • 本頁收集關于VBS類構造函數與Default關鍵字使用介紹的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 张掖市| 德清县| 青神县| 海原县| 高青县| 卢湾区| 庆元县| 正宁县| 阿拉善左旗| 恩施市| 那坡县| 南安市| 上栗县| 淄博市| 永吉县| 鄂伦春自治旗| 南投市| 广丰县| 涿州市| 涟水县| 临城县| 建瓯市| 磐石市| 西乌| 洛宁县| 高雄县| 宣恩县| 丁青县| 泾川县| 安顺市| 苏尼特右旗| 德州市| 镇赉县| 三都| 闽清县| 翼城县| 和平区| 贺州市| 白河县| 衡阳县| 宕昌县|