2.1.5 获取对象引用GetRef方法
在本小节中,为读者简单介绍一下VBS中的函数指针,我们都知道函数指针是程序算法的一部分,它和数组一样也需要占用一部分的存储空间,也都有相应的地址。不但可以使用指针变量指向数组的首地址,同样也可以使用指针指向函数代码的首地址,我们把指向函数代码首地址的指针变量称为函数指针。
GetRef:它所提供的功能被称为函数指针,即它指向了在指定事件发生时要执行的过程的地址。
实例:
'变量强制申明 Option Explicit '定义变量 Dim new_helloworld '定义helloworld函数 Function helloworld (content) MsgBox content End Function '使变量new_helloworld指向helloworld函数 Set new_helloworld = GetRef("helloworld") ‘调用new_helloworld |
上面这段代码是把变量new_helloworld的指针指向helloworld函数,这样new_helloworld就具有了helloworld函数的功能,放到QTP中执行以后的结果如图2-11所示。
图2-11
如图2-11所示,指针成功指向了helloworld函数,并最终成功调用了MsgBox方法。
2.1.6 类的简单应用
“类”这个名词应该是不陌生的,但是很多自动化测试新人往往不知道,其实在VBScript中一样可以使用Class“类”!而且Class“类”在自动化测试中相当常用,对于代码量增大时,类的结构化就充分体现出了它强大的优势,下面就来看一下类的组成部分以及它们的一些用法。
1.初始化与终结化的应用
Class User Private Sub Class_Initialize '当这个类被创建时执行 End Sub Private Sub Class_Terminate '当类被销毁时执行 End Sub End Class |
2.Get与Set的应用
Class User '************定义变量名************* Private s_name Private s_age '************定义Get方法************ Public Property Get name name = s_name End Property Public Property Get age age = s_age End Property '************定义Set方法************ Public Property Let name (new_name) s_name = new_name End Property Public Property Let age (new_age) s_age = new_age End Property End Class Set user1 = New user user1.age = "100" user1.name = "拉生" MsgBox "姓名:" +user1.name+ " 年龄:" +user1.age |