在VBScript中使用对象

工作需要,用vbs写了个小工具,回归原始了一把。 鉴于本人长期使用java,与对象异常亲热。 今天也试了下在VBScript中使用对象,感觉不错!有兴趣的朋友可以自己试试

 

[一] VBScript对象声明

 

Class ClassName
    ' Fields, Functions, Properties go here.
End Class

 

其中ClassName为类名,创建对象的语句也很简单,即: Set obj = new ClassName

 

 

 

[二] 对象中的成员

 

常用的即public成员与private成员,private成员只能在类内部可见。 以Person类为例,我们定义了如下4个成员变量。私有变量前加"m_"为VBScript通常的命名规约。

 

 

 

 

Class Person
    public name
    private m_age
    private m_gender
    private m_bestFriend
End Class

 

 

 外部调用public变量时,可直接用.[变量名], 若我们想为Person定义名字为John,即:

Set John = new Person

John.name = "John"

 

[三] 对象中Properties的Let, Set与Get

 

在VBScript脚本中,我们通常把成员变量称之为Properties,我们可以为它定义Let, Set, Get Procedure。 由于我们经常会将变量设为private的,因此这三个方法就显得很重要了。 其中若Property为普通变量,则用Let进行设值,若为对象则需要定义Set。 取值直接用Get。

 

Class Person
public name
private m_age
private m_gender
private m_bestFriend

'==========================================================================
' Name: age
' Summary: Let & Get method m_age
'==========================================================================
public Property Get age()
    age = m_age
End Property

public Property Let age(intAge)
    m_age = intAge
End Property

'==========================================================================
' Name: gender
' Summary: Let & Get method m_gender
'==========================================================================
public Property Get gender()
    gender = m_gender
End Property

public Property Let gender(strGender)
    m_gender = strGender
End Property 

'==========================================================================
' Name: bestFriend
' Summary: Set & Get method m_bestFriend
'==========================================================================
public Property Get bestFriend()
    Set bestFriend = m_bestFriend
End Property

public Property Set bestFriend(objFriend)
    Set m_bestFriend = objFriend
End Property 

End Class

 

 

调用Let/Set/Get, 例子如下:

Set John = new Person

John.name = "John"

John.age = 25

John.gender = "Male"

 

Set Annie = new Person

Annie.name = "Annie"

Annie.age = 23

Annie.gender = "Female"

 

Set John.bestFriend = Annie

wscript.echo "John's best friend: " & John.bestFriend.name

 

[四] 对象中定义函数

 

与java/c++一样VBScript也支持在对象内部定义函数。 格式与用法都很简单。 给个加好友的例子。

 

 

Class Person
public name
private m_friends

'==========================================================================
' Name: addFriend
' Summary: Add a friend to Person
'==========================================================================
Public Function addFriend(objFriend)

    If (NOT IsEmpty(m_friends)) Then
        ' Use Preserve keyword to avoid erasing while Redim.
        ReDim Preserve m_friends(UBound(m_friends) + 1)
    Else
        ReDim m_friends(0)
    End If

     ' Add Friend
    Set m_friends(UBound(m_friends)) = objFriend
End Function 

'==========================================================================
' Name: getFriends
' Summary: Get all the friends
'==========================================================================
Public Function getFriends()
    getFriends = m_friends
End Function

End Class

 

 

测试例子:

Set John = new Person

John.name = "John"

Set Annie = new Person

Annie.name = "Annie"

Set Michael= new Person

Annie.name = "Michael"

 

John.addFriend(Annie)

John.addFriend(Michael)

 

wscript.echo "John has " & UBound(John.getFriends()) + 1 & " friends."

 

 

Hope it can relax your eyes : )

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值