其实 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
复制代码 代码如下:
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
标签:
构造函数,Default,关键字
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“VBS类构造函数与Default关键字使用介绍”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2024年05月11日
2024年05月11日
- 孟庭苇《国语真经典 环球精选华语专辑》[WAV+CUE][770MB]
- 张学友《饿狼传说 环球20周年 劲爆销量大奖》[WAV+CUE][440MB]
- 赵鹏 《震撼2020 头版限量 24K金碟》[WAV+CUE][530MB]
- 黄克林.1992-少年甭捉狂【龙吟唱片】【WAV+CUE】
- 冯玮君.2003-Joanna【BMG】【WAV+CUE】
- 窦佳嫄.2024-DCHO【黑籁音乐】【FLAC分轨】
- 逆水寒手游五子棋第二十关怎么过 逆水寒手游残局攻略大全
- 物华弥新迷踪盘第八关怎么过 迷踪盘第八关通关方法
- 物华弥新迷踪盘第九关怎么过 迷踪盘第九关通关方法
- [ABC唱片]-《雪莉—最爱十年》[HD-142][德国版HD]WAV+CUE]
- [ABC唱片]-《雪莉—色》[HDS-143][6N纯银镀膜CD]WAV+CUE]
- 郑华娟1995-旅途与岁月的纪念品(2013华纳复刻版)[飞碟][WAV+CUE]
- 群星.1997-97滚石年度最卖座主打歌刷新全纪录·男女歌合战【滚石】【WAV+CUE】
- 甄妮.1997-20年长情金曲2CD(南方金点系列)【南方】【WAV+CU
- 巫启贤.1998-巫启贤的咖啡鸟2CD【南方】【WAV+CUE】