在VBScript中使用类(一),VBScript,脚本特效

来源:百度文库 编辑:神马文学网 时间:2024/04/27 16:56:33
在vbscript中使用类
前言
首先,在我进入实质性主题并解释如何建立类之前,我希望保证你知道“对象”。虽然你可以在程序中使用对象而不用知道其正确的规则,但我并不建议如此!对于对象的初学者,接下来的部分将让你了解其概念及内容。已经了解面向对象编程(oop)的读者可以跳过这章节。
导论
l         “对象是什么?”——对象通常代表某种实体,主要是一个变量和函数的集合。
l         “实体是什么?”——字面上说,实体是一个“事物”,我的意思是一个概念或者任何一个物体。例如,一辆汽车是一个实体,因为它是一个物体。你公司销售部门销售产品也是一个实体,当然,你也可以将其拆开来看,销售人员、客户、产品等都是实体。
让我们更深入的来看“销售”这个实体(对象)。为了使你更准确地有一个销售的“映像”,你需要知道客户买了什么,是哪个客户,谁是销售人员等等……这看来是一个简单的事件,但假设所有信息是存储在单独的数据库表中的,那么当你需要获得某个销售过程所有相关信息时,你必须在你的数据库中做多次独立查询,再将所有的数据集拢。有没有更简便的办法而一次获得销售的所有信息呢?“对象”。
在对象中,你可以植入代码以从其他表中获得数据,你也可以保存对象属性的所有信息,这样,你可以轻松地使用代码管理你的销售数据。例如:
‘open the database connectionset objconn = server.createobject("adodb.connection")objconn.open "mydsn" ‘create the recordset objectset objrs = server.createobject("adodb.recordset") ‘define the sql querystrcomplexsqlquery = "select c.name, s.name from customers c, " & _        "salespeople s, sales sl where sl.customerid=c.id and " & _         "sl.salespersonid=s.id and sl.id=" & stridofthissale & ";" ‘open the recordsetobjrs.open strcomplexsqlquery, objconn, adopenforwardonly, _               adlockreadonly, adcmdtext ‘take the customer and sales person names from the recordsetstrcustomername = objrs(0)strsalespersonname = objrs(1) ‘tidy up the objectsobjrs.closeobjconn.closeset objrs = nothingset objconn = nothing ‘output the dataresponse.write "this sale was made by " & strsalespersonname & _               " to " & strcustomername
可以使用“对象”来替代:
‘create the "sale" objectset objsale = new sale ‘lookup the correct saleobjsale.id = stridofthissale ‘output the dataresponse.write "this sale was made by " & objsale.salespersonname & _               " to " & objsale.customername ‘tidy up the objectsobjsale.closeset objsale = nothing如果你使用“sale”对象做比打印更多的事,可以让你省去很多的打字时间。
计算中,对象包括“属性”和“方法”。属性主要是储存在对象中的一个变量,其用法与变量相同。唯一的区别在于参数赋值为:strmyvar = "this is a string variant", 而对象属性为 objobject.property="this is a string variant"。这点非常简单而有用处。方法可以理解为植入对象中的函数与过程,可以使用strmyvar = objobject.methodname(strmyvar)来代替strmyvar =functionname(strmyvar)。写法不同,但功能相同。属性的一个例子是对象response中的expireabsolute,response.expiresabsolute = cdate("1 september 1999")。方法的一个例子是对象response中的write方法,response.write "hello world!"。
vbscript的一个新特性就是其可以创建新的对象而不需要求诸于花销时间都极大的编译器。我将向读者展示如何创建对象的类,并希望提供一个良好的开端。