最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

VBS類構(gòu)造函數(shù)與Default關(guān)鍵字使用介紹

 更新時間:2011年10月26日 21:59:54   作者:  
很多人并不知道 VBS 中還有個 Default 關(guān)鍵字,更不用說知道 Default 關(guān)鍵字的用法。
其實 MSDN 的 VBScript 文檔中關(guān)于 Function 和 Sub 語句的部分提到過 Default 關(guān)鍵字:
復(fù)制代碼 代碼如下:

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ù)過程是類的默認方法。如果類中一個以上的過程被定義為 Default,那么會出現(xiàn)錯誤。
一個簡單的例子:
復(fù)制代碼 代碼如下:

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ù)的概念,只是提供了一個類初始化事件來初始化對象:
復(fù)制代碼 代碼如下:

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: 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

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

相關(guān)文章

最新評論

视频| 古浪县| 浮梁县| 沙河市| 榆社县| 航空| 高密市| 瑞金市| 安溪县| 长海县| 富源县| 龙山县| 奉贤区| 酒泉市| 林甸县| 绵阳市| 中卫市| 长垣县| 威信县| 固镇县| 武鸣县| 长顺县| 五峰| 蓝山县| 澄迈县| 夹江县| 凤翔县| 黔西县| 庆城县| 班戈县| 赞皇县| 满城县| 通渭县| 济南市| 安丘市| 永年县| 拉萨市| 安远县| 秀山| 沐川县| 高碑店市|