1在VBScript中使用类前言
  2在VBScript中使用类首先,在我进入实质性主题并解释如何建立类之前,我希望保证你知道“对象”。虽然你可以在程序中使用对象而不用知道其正确的规则,但我并不建议如此!对于对象的初学者,接下来的部分将让你了解其概念及内容。已经了解面向对象编程(OOP)的读者可以跳过这章节。 
  3在VBScript中使用类      导论
  4在VBScript中使用类        “对象是什么?”——对象通常代表某种实体,主要是一个变量和函数的集合。
  5在VBScript中使用类        “实体是什么?”——字面上说,实体是一个“事物”,我的意思是一个概念或者任何一个物体。例如,一辆汽车是一个实体,因为它是一个物体。你公司销售部门销售产品也是一个实体,当然,你也可以将其拆开来看,销售人员、客户、产品等都是实体。
  6在VBScript中使用类让我们更深入的来看“销售”这个实体(对象)。为了使你更准确地有一个销售的“映像”,你需要知道客户买了什么,是哪个客户,谁是销售人员等等……这看来是一个简单的事件,但假设所有信息是存储在单独的数据库表中的,那么当你需要获得某个销售过程所有相关信息时,你必须在你的数据库中做多次独立查询,再将所有的数据集拢。有没有更简便的办法而一次获得销售的所有信息呢?“对象”。
  7在VBScript中使用类     在对象中,你可以植入代码以从其他表中获得数据,你也可以保存对象属性的所有信息,这样,你可以轻松地使用代码管理你的销售数据。例如:
  8在VBScript中使用类
  9在VBScript中使用类'Open the database connection
 10在VBScript中使用类Set objConn = Server.CreateObject("ADODB.Connection")
 11在VBScript中使用类objConn.Open "MyDSN"
 12在VBScript中使用类
 13在VBScript中使用类'Create the recordset object
 14在VBScript中使用类Set objRS = Server.CreateObject("ADODB.Recordset")
 15在VBScript中使用类
 16在VBScript中使用类'Define the SQL query
 17在VBScript中使用类strComplexSQLQuery = "SELECT C.Name, S.Name FROM Customers C, " & _
 18在VBScript中使用类        "Salespeople S, Sales Sl WHERE Sl.CustomerID=C.ID AND " & _
 19在VBScript中使用类         "Sl.SalespersonID=S.ID AND Sl.ID=" & strIDOfThisSale & ";"
 20在VBScript中使用类
 21在VBScript中使用类'Open the recordset
 22在VBScript中使用类objRS.Open strComplexSQLQuery, objConn, adOpenForwardOnly, _
 23在VBScript中使用类               adLockReadOnly, adCmdText
 24在VBScript中使用类
 25在VBScript中使用类'Take the customer and sales person names from the recordset
 26在VBScript中使用类strCustomerName = objRS(0)
 27在VBScript中使用类strSalesPersonName = objRS(1)
 28在VBScript中使用类
 29在VBScript中使用类'Tidy up the objects
 30在VBScript中使用类objRS.Close
 31在VBScript中使用类objConn.Close
 32在VBScript中使用类Set objRS = Nothing
 33在VBScript中使用类Set objConn = Nothing
 34在VBScript中使用类
 35在VBScript中使用类'Output the data
 36在VBScript中使用类Response.Write "This sale was made by " & strSalesPersonName & _
 37在VBScript中使用类               " to " & strCustomerName
 38在VBScript中使用类
 39在VBScript中使用类
 40在VBScript中使用类可以使用“对象”来替代:
 41在VBScript中使用类
 42在VBScript中使用类'Create the "Sale" object
 43在VBScript中使用类Set objSale = New Sale
 44在VBScript中使用类
 45在VBScript中使用类'Lookup the correct sale
 46在VBScript中使用类objSale.ID = strIDOfThisSale
 47在VBScript中使用类
 48在VBScript中使用类'Output the data
 49在VBScript中使用类Response.Write "This sale was made by " & objSale.SalesPersonName & _
 50在VBScript中使用类               " to " & objSale.CustomerName
 51在VBScript中使用类
 52在VBScript中使用类'Tidy up the objects
 53在VBScript中使用类objSale.Close
 54在VBScript中使用类Set objSale = Nothing
 55在VBScript中使用类如果你使用“Sale”对象做比打印更多的事,可以让你省去很多的打字时间。
 56在VBScript中使用类
 57在VBScript中使用类计算中,对象包括“属性”和“方法”。属性主要是储存在对象中的一个变量,其用法与变量相同。唯一的区别在于参数赋值为: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!"
 58在VBScript中使用类    VBScript的一个新特性就是其可以创建新的对象而不需要求诸于花销时间都极大的编译器。我将向读者展示如何创建对象的类,并希望提供一个良好的开端。
 59在VBScript中使用类
 60在VBScript中使用类    在VBScript中使用类(二)
 61在VBScript中使用类     创建对象
 62在VBScript中使用类在VBScript中创建对象类型(类)时,你首先要知道,这真的很容易!我在一个下午自学,只是阅读了Microsof VB Script 的参考书,但必须承认,这书不是最容易阅读的文档。
 63在VBScript中使用类    初学者需要安装VBScript 5.0引擎,可以在Microsoft's Scripting Site下载。
 64在VBScript中使用类我们来看代码。类的定义与函数和子过程非常类似。起始行为Class <MyClassName>,结尾是End Class,所有的对象定义写在中间部门。现在我们可以用所学的来建立第一个类,不实现任何功能的类。
 65在VBScript中使用类Class 4GuysTestObject
 66在VBScript中使用类'你的ASP代码
 67在VBScript中使用类End Class
 68在VBScript中使用类     这看上去不是那么回事,但当你写入下面的代码后,你将创建一个对象的实例:
 69在VBScript中使用类Dim objTestObject
 70在VBScript中使用类Set objTestObject = New 4GuysTestObject
 71在VBScript中使用类Set objTestObject = Nothing
 72在VBScript中使用类    显然,我们现在还不能用对象做任何事,现在我将解释如何在对象中定义属性和方法。
 73在VBScript中使用类   使用对象可以做的最基础的,是建立一组数据。例如,假如要将时间、日期及视频程序标题建立在一起,你可以创建一个包含属性“StartTime”, “ProgramDate”和“ProgramTitle”的对象。代码如下:
 74在VBScript中使用类
 75在VBScript中使用类Class TVProgram
 76在VBScript中使用类
 77在VBScript中使用类Public StartTime
 78在VBScript中使用类Public ProgramDate
 79在VBScript中使用类Public ProgramTitle
 80在VBScript中使用类
 81在VBScript中使用类End Class
 82在VBScript中使用类
 83在VBScript中使用类Dim objTVShow
 84在VBScript中使用类Set objTVShow = New TVProgram
 85在VBScript中使用类
 86在VBScript中使用类objTVShow.StartTime = CDate("17:30")
 87在VBScript中使用类objTVShow.ProgramDate = DateSerial(1999,9,17)
 88在VBScript中使用类objTVShow.ProgramTitle = "The Jerry Springer Show"
 89在VBScript中使用类
 90在VBScript中使用类Response.Write objTVShow.ProgramTitle & " is on at " & _
 91在VBScript中使用类objTVShow.StartTime & " on " & objTVShow.ProgramDate
 92在VBScript中使用类
 93在VBScript中使用类代码工作的方式是,我们定义StartTime,ProgramDate和ProgramTitle为类TVProgram的属性。这样,这些属性就像其他变量一样来处理,没有设置值则不会执行代码。属性名字前的Public字段有其真实含义,而且非常重要。如果你不特指一个方法或属性为public或private,系统默认值为public,但最好养成定义任何值的良好书写习惯(也方便你之后自己的阅读)。
 94在VBScript中使用类
 95在VBScript中使用类    上面程序的结果大致如下(决定于你的本地服务器配置):
 96在VBScript中使用类
 97在VBScript中使用类The Jerry Springer Show is on at 5:30pm on 17/09/99.
 98在VBScript中使用类
 99在VBScript中使用类    我在英国,所以日期现实如上。不论你跑什么工程,它的效果都不错,但只有你开始使用其他对象的功能,为你可能需要的所有信息和功能,创建一个完美的接口,以支持你所建对象包围的实体,你才会体会到对象的真正实力。
100在VBScript中使用类    现在,如果你不喜欢上面例子显示日期的方法,而希望以同一种格式现实日期,也没有不要在引用每个ProgramDate属性时加FormatDateTime(),你只需要将此类代码植入属性本身。
101在VBScript中使用类
102在VBScript中使用类    这样需要用另一种方法定义属性。同样,我们将使用ProgramDate为外部可见属性,但因为ProgramDate属性将成为一个函数而不是静态值,我们将实际日期保存在另一个属性中internal_ProgramDate。
103在VBScript中使用类
104在VBScript中使用类Class TVProgram
105在VBScript中使用类
106在VBScript中使用类Public StartTime
107在VBScript中使用类Public internal_ProgramDate
108在VBScript中使用类
109在VBScript中使用类Public Property Get ProgramDate
110在VBScript中使用类ProgramDate = Day(internal_ProgramDate) & _
111在VBScript中使用类" " & MonthName(Month(internal_ProgramDate)) & _
112在VBScript中使用类" " & Year(internal_ProgramDate)
113在VBScript中使用类End Property
114在VBScript中使用类
115在VBScript中使用类               Public ProgramTitle
116在VBScript中使用类        End Class
117在VBScript中使用类
118在VBScript中使用类Dim objTVShow
119在VBScript中使用类Set objTVShow = New TVProgram
120在VBScript中使用类
121在VBScript中使用类objTVShow.StartTime = CDate("17:30")
122在VBScript中使用类objTVShow.internal_ProgramDate = DateSerial(1999,9,17)
123在VBScript中使用类objTVShow.ProgramTitle = "The Jerry Springer Show"
124在VBScript中使用类
125在VBScript中使用类Response.Write objTVShow.ProgramTitle & " is on at " & _
126在VBScript中使用类objTVShow.StartTime & " on " & objTVShow.ProgramDate & "."
127在VBScript中使用类
128在VBScript中使用类
129在VBScript中使用类程序的结果如下:
130在VBScript中使用类
131在VBScript中使用类The Jerry Springer Show is on at 5:30pm on 17 September 1999.
132在VBScript中使用类
133在VBScript中使用类   在VBScript中使用类(三)
134在VBScript中使用类
135在VBScript中使用类我们来分析一下(2)中的程序:
136在VBScript中使用类
137在VBScript中使用类Class TVProgram
138在VBScript中使用类
139在VBScript中使用类               Public StartTime
140在VBScript中使用类
141在VBScript中使用类               Public internal_ProgramDate
142在VBScript中使用类
143在VBScript中使用类               Public Property Get ProgramDate
144在VBScript中使用类
145在VBScript中使用类                       ProgramDate = Day(internal_ProgramDate) & _
146在VBScript中使用类
147在VBScript中使用类                               " " & MonthName(Month(internal_ProgramDate)) & _
148在VBScript中使用类
149在VBScript中使用类                               " " & Year(internal_ProgramDate)
150在VBScript中使用类
151在VBScript中使用类               End Property
152在VBScript中使用类
153在VBScript中使用类               Public ProgramTitle
154在VBScript中使用类
155在VBScript中使用类        End Class
156在VBScript中使用类
157在VBScript中使用类        Dim objTVShow
158在VBScript中使用类
159在VBScript中使用类        Set objTVShow = New TVProgram
160在VBScript中使用类
161在VBScript中使用类        objTVShow.StartTime = CDate("17:30")
162在VBScript中使用类
163在VBScript中使用类        objTVShow.internal_ProgramDate = DateSerial(1999,9,17)
164在VBScript中使用类
165在VBScript中使用类        objTVShow.ProgramTitle = "The Jerry Springer Show"
166在VBScript中使用类
167在VBScript中使用类        Response.Write objTVShow.ProgramTitle & " is on at " & _
168在VBScript中使用类
169在VBScript中使用类       objTVShow.StartTime & " on " & objTVShow.ProgramDate & "."
170在VBScript中使用类
171在VBScript中使用类当调用对象的属性ProgramDate时,实际上执行了函数ProgramDate,即如上定义的函数,而很快你也将习惯这种在声明部分使用Public或Private关键字的方式。关键字“Property”,告知了编译器如同调用属性一样在外部调用函数。接着的“Get”,表明该函数是输出还是获得一个值。
172在VBScript中使用类
173在VBScript中使用类Get的意思是“允许外部代码去‘获取’一个值”,与其类似的关键字还有“Let”和“Set”,但这两个比较复杂,因而我们以后再讨论。
174在VBScript中使用类
175在VBScript中使用类接下去的代码看来有点难度的,给objectname.internal_ProgramDate赋值并通过objectname.ProgramDate来调用它。如果可以使用相同关键字同时为其赋值并获得它的值不是更好吗?当然,那也可以。
176在VBScript中使用类
177在VBScript中使用类如果定义Get和Let属性的名称相同,可以将它们当作对象相同的属性,但这只限于它们定义了相同数量的成员。(以下代码看来不太相同,仅作为实例参考)
178在VBScript中使用类
179在VBScript中使用类Class TVProgram
180在VBScript中使用类
181在VBScript中使用类               Public StartTime
182在VBScript中使用类
183在VBScript中使用类               Public internal_ProgramDate
184在VBScript中使用类
185在VBScript中使用类               Public Property Get ProgramDate
186在VBScript中使用类
187在VBScript中使用类                       ProgramDate = Day(internal_ProgramDate) & " " _
188在VBScript中使用类
189在VBScript中使用类                               & MonthName(Month(internal_ProgramDate)) & _
190在VBScript中使用类
191在VBScript中使用类                               " " & Year(internal_ProgramDate)
192在VBScript中使用类
193在VBScript中使用类               End Property
194在VBScript中使用类
195在VBScript中使用类               Public Property Let ProgramDate(ByVal varDateIn)
196在VBScript中使用类
197在VBScript中使用类                       internal_ProgramDate = CDate(varDateIn)
198在VBScript中使用类
199在VBScript中使用类               End Property
200在VBScript中使用类
201在VBScript中使用类               Public ProgramTitle
202在VBScript中使用类
203在VBScript中使用类        End Class
204在VBScript中使用类
205在VBScript中使用类        Dim objTVShow
206在VBScript中使用类
207在VBScript中使用类        Set objTVShow = New TVProgram
208在VBScript中使用类
209在VBScript中使用类        objTVShow.StartTime = CDate("17:30")
210在VBScript中使用类
211在VBScript中使用类        objTVShow.ProgramDate = "17 Sept 99"
212在VBScript中使用类
213在VBScript中使用类        objTVShow.ProgramTitle = "The Jerry Springer Show"
214在VBScript中使用类
215在VBScript中使用类    Response.Write objTVShow.ProgramTitle & " is on at " & _
216在VBScript中使用类
217在VBScript中使用类       objTVShow.StartTime & " on " & objTVShow.ProgramDate & "."
218在VBScript中使用类
219在VBScript中使用类以上代码中Let的声明部分看来似乎是一个多余的元素,当我第一次看到时研究了很长时间。每次我使用“0”作为变量用在每个属性上,我总是得到这个错误信息,“元素数量必须相等”。“它们确实相等!”抓狂之后,我回过头去看程序才觉得自己的愚蠢!:)
220在VBScript中使用类
221在VBScript中使用类原因是,当你试图为ProgramDate赋值时,你会使用这样一行程序:
222在VBScript中使用类
223在VBScript中使用类objTVShow.ProgramDate = dtmMyDate
224在VBScript中使用类
225在VBScript中使用类为了方便,等号右边的值(这里指dtmMyDate)作为了一个程元赋给了函数。因此编译器可能会认为在Get ProgramDate行有0程元,而Let ProgramDate却多一个!分配的值总是被略过而作为属性的最后一个程元,所以即使你使用其他程元,所赋的值总是作为最后一个程元。
226在VBScript中使用类
227在VBScript中使用类现在看程序。无论通过ProgramDate设置日期为文本形式,还是用internal_ProgramDate译成日期变量,程序都没有问题。但能不能只使用一个入口呢?
228在VBScript中使用类
229在VBScript中使用类如果internal_ProgramDate只能在内部有效,而使用Let ProgramDate检查传输的数据类型,我们就可以作出选择。例如:
230在VBScript中使用类
231在VBScript中使用类Class TVProgram
232在VBScript中使用类
233在VBScript中使用类               Public StartTime
234在VBScript中使用类
235在VBScript中使用类               Private internal_ProgramDate
236在VBScript中使用类
237在VBScript中使用类               Public Property Get ProgramDate
238在VBScript中使用类
239在VBScript中使用类                       ProgramDate = Day(internal_ProgramDate) & " " & _
240在VBScript中使用类
241在VBScript中使用类                               MonthName(Month(internal_ProgramDate)) & _
242在VBScript中使用类
243在VBScript中使用类                               " " & Year(internal_ProgramDate)
244在VBScript中使用类
245在VBScript中使用类               End Property
246在VBScript中使用类
247在VBScript中使用类               Public Property Let ProgramDate(ByVal varDateIn)
248在VBScript中使用类
249在VBScript中使用类                       If IsDate(varDateIn) Then
250在VBScript中使用类
251在VBScript中使用类                               internal_ProgramDate = varDateIn
252在VBScript中使用类
253在VBScript中使用类                       Else
254在VBScript中使用类
255在VBScript中使用类                               'Place some error handling code in here.
256在VBScript中使用类
257在VBScript中使用类                       End If
258在VBScript中使用类
259在VBScript中使用类               End Property
260在VBScript中使用类
261在VBScript中使用类               Public ProgramTitle
262在VBScript中使用类
263在VBScript中使用类        End Class
264在VBScript中使用类
265在VBScript中使用类并同样声明StartTime属性:
266在VBScript中使用类
267在VBScript中使用类Class TVProgram
268在VBScript中使用类
269在VBScript中使用类               Private internal_StartTime
270在VBScript中使用类
271在VBScript中使用类               Public Property Get StartTime
272在VBScript中使用类
273在VBScript中使用类                       StartTime = Hour(internal_StartTime) & ":" _
274在VBScript中使用类
275在VBScript中使用类                               & Minute(internal_StartTime)
276在VBScript中使用类
277在VBScript中使用类               End Property
278在VBScript中使用类
279在VBScript中使用类               Public Property Let StartTime(ByVal varTimeIn)
280在VBScript中使用类
281在VBScript中使用类                       If IsDate(varTimeIn) Then
282在VBScript中使用类
283在VBScript中使用类                               internal_StartTime = varTimeIn
284在VBScript中使用类
285在VBScript中使用类                       End If
286在VBScript中使用类
287在VBScript中使用类               End Property
288在VBScript中使用类
289在VBScript中使用类               Private internal_ProgramDate
290在VBScript中使用类
291在VBScript中使用类               Public Property Get ProgramDate
292在VBScript中使用类
293在VBScript中使用类                       ProgramDate = Day(internal_ProgramDate) & " " _
294在VBScript中使用类
295在VBScript中使用类                               & MonthName(Month(internal_ProgramDate)) & _
296在VBScript中使用类
297在VBScript中使用类                               " " & Year(internal_ProgramDate)
298在VBScript中使用类
299在VBScript中使用类               End Property
300在VBScript中使用类
301在VBScript中使用类               Public Property Let ProgramDate(ByVal varDateIn)
302在VBScript中使用类
303在VBScript中使用类                       If IsDate(varDateIn) Then
304在VBScript中使用类
305在VBScript中使用类                               internal_ProgramDate = varDateIn
306在VBScript中使用类
307在VBScript中使用类                       End If
308在VBScript中使用类
309在VBScript中使用类               End Property
310在VBScript中使用类
311在VBScript中使用类               Public ProgramTitle
312在VBScript中使用类
313在VBScript中使用类        End Class
314在VBScript中使用类        在VBScript中使用类
315在VBScript中使用类    现在的代码离我们想要的还是有些不太实用,我们将在其他页使用类TVProgram,因此最好将其独立定义,以便所有也面都可以调用。我们将在第四部分讨论这点。
316在VBScript中使用类      在VBScript中使用类(四)
317在VBScript中使用类
318在VBScript中使用类现在的代码离我们想要的还是有些不太实用,我们将在其他页使用类TVProgram,因此最好将其独立定义,以便所有也面都可以调用。创建一个ASP页面,并命名为TVProgramClass.asp,我们在其中定义类TVProgram。
319在VBScript中使用类--TVProgramClass.asp--
320在VBScript中使用类<%
321在VBScript中使用类        Class TVProgram
322在VBScript中使用类               Private internal_StartTime
323在VBScript中使用类               Public Property Get StartTime
324在VBScript中使用类
325在VBScript中使用类                       StartTime = Hour(internal_StartTime) & _
326在VBScript中使用类
327在VBScript中使用类                               ":" & Minute(internal_StartTime)
328在VBScript中使用类
329在VBScript中使用类               End Property
330在VBScript中使用类               Public Property Let StartTime(ByVal varTimeIn)
331在VBScript中使用类
332在VBScript中使用类                       If IsDate(varTimeIn) Then
333在VBScript中使用类
334在VBScript中使用类                               internal_StartTime = varTimeIn
335在VBScript中使用类
336在VBScript中使用类                       End If
337在VBScript中使用类
338在VBScript中使用类               End Property
339在VBScript中使用类               Private internal_ProgramDate
340在VBScript中使用类               Public Property Get ProgramDate
341在VBScript中使用类                       ProgramDate = Day(internal_ProgramDate) & _
342在VBScript中使用类                              " " & MonthName(Month(internal_ProgramDate)) & _
343在VBScript中使用类
344在VBScript中使用类                               " " & Year(internal_ProgramDate)
345在VBScript中使用类
346在VBScript中使用类               End Property
347在VBScript中使用类
348在VBScript中使用类               Public Property Let ProgramDate(ByVal varDateIn)
349在VBScript中使用类                       If IsDate(varDateIn) Then
350在VBScript中使用类                               internal_ProgramDate = varDateIn
351在VBScript中使用类                       End If
352在VBScript中使用类               End Property
353在VBScript中使用类
354在VBScript中使用类               Public ProgramTitle
355在VBScript中使用类
356在VBScript中使用类        End Class
357在VBScript中使用类%>
358在VBScript中使用类这样就可以在任何ASP中调用我们定义的类了,语法如下:
359在VBScript中使用类<!-- #include virtual="TVProgramClass.asp" -->
360在VBScript中使用类<%
361在VBScript中使用类        Dim objTVShow
362在VBScript中使用类        Set objTVShow = New TVProgram
363在VBScript中使用类
364在VBScript中使用类        objTVShow.StartTime = CDate("17:30")
365在VBScript中使用类        objTVShow.ProgramDate = DateSerial(1999,9,17)
366在VBScript中使用类        objTVShow.ProgramTitle = "The Jerry Springer Show"
367在VBScript中使用类
368在VBScript中使用类%> 
369在VBScript中使用类<%= objTVShow.ProgramTitle %> is on at <%= objTVShow.StartTime %> on <%= objTVShow.ProgramDate %>.
370在VBScript中使用类这里有一个建议。如果你重命名你的包含文件.asp,并保证所有重要代码都在<CODE><在VBScript中使用类 %>< CODE>中,那么就算有人猜到了你包含文件的文件名,也没办法看见里面的内容!
371在VBScript中使用类
372在VBScript中使用类评论:
373在VBScript中使用类    VBScript类在封装的层面上,还是有不少好处的,增加了代码的可读性与可维护性以及清晰度,但是,没有继承和重载等特性,并不是真正意义上的OOP。没有集合,但是据说可以用Scripting.Dictionary,据说更好用、更快。不过,感觉总是鸡类(肋)。

相关文章:

  • 2021-10-26
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
相关资源
相似解决方案