在ASP中使用类(Class)
1
在ASP中使用类(class)
2
在不入前的一天,当我为了解决一个语法问题来翻阅VBscript文档时,偶然间发现在了下面的一句话:
3![](/Images/OutliningIndicators/None.gif)
4
Class Statement
5![](/Images/OutliningIndicators/None.gif)
6
Declares the name of a class, as well as a definition of the variables, properties, and methods that comprise the class.
7![](/Images/OutliningIndicators/None.gif)
8
翻译过来就是![](/Images/dot.gif)
![](/Images/dot.gif)
9![](/Images/OutliningIndicators/None.gif)
10
Class 声明
11![](/Images/OutliningIndicators/None.gif)
12
声明一个类的名字,就是定义一些变量,属性,方法来组成一个类
13![](/Images/OutliningIndicators/None.gif)
14
这是真的!!!?VBScript中能用类!?!?不知道能不能用于ASP!?这样的话,我就不是能写出像object一样的ASP程序?!说干就干!实践是检验真理的唯一标准,自个动手吧!
15![](/Images/OutliningIndicators/None.gif)
16
我们常常看到别的程序语言中中都有类的说明,PHP,VB,C++,这个在VBScript中的类的说明,我是第一次听到,我们的日常工作就是网站开发,在这个里面多多少少搞出点经验,像模像样也能自诩为"内行",所以我就来分享一下我所知道的这个新的东东。我们来看看下面的这个代码吧!(window2000+IIS5.0通过测试)
17![](/Images/OutliningIndicators/None.gif)
18
<%
19
''声明一个名为aspcn的类
20
Class aspcn
21
Private aspcn
22
''初始化类
23
Private Sub Class_Initialize
24
aspcn="Aspcn Is Good!<br>"
25
End Sub
26
''定义一个函数
27
Public Function DoIt()
28
DoIt=aspcn
29
End Function
30
''定义一个方法
31
Public Sub QueryStr(stat)
32
Response.write stat
33
End Sub
34![](/Images/OutliningIndicators/None.gif)
35
End Class
36![](/Images/OutliningIndicators/None.gif)
37
Set Hi_aspcn=New aspcn ''定义一个名为Hi_aspcn的aspcn对象实例
38
response.write Hi_aspcn.DoIt
39
varstr="Aspcn Is Cool!<br><font color=red>http://www.aspcn.com</font><br>WelCome!!!"
40
Hi_aspcn.QueryStr varstr
41![](/Images/OutliningIndicators/None.gif)
42
%>
43![](/Images/OutliningIndicators/None.gif)
44![](/Images/OutliningIndicators/None.gif)
45
这是很简单的一个程序,我们在其中声明了一个名为aspcn的类,建立了一个DoIt函数,一个QueryStr方法,这个程序很简单相信大家能看懂,它的显示如下:
46![](/Images/OutliningIndicators/None.gif)
47
Aspcn Is Good!
48
Aspcn Is Cool!
49
http://www.aspcn.com/
50
WelCome!!!
51![](/Images/OutliningIndicators/None.gif)
52
以后,咱们就可以把我们常用到的程序写成一个类,到时候就用<!--#include file="xxx.asp"-->来包含进来就行了,这给我们开发程序又提供了新的空间,真是爽啊!和C++一样了,有点回归自然的感觉.
53![](/Images/OutliningIndicators/None.gif)
54![](/Images/OutliningIndicators/None.gif)
55
VBSctipt 5.0中的新特性
56![](/Images/OutliningIndicators/None.gif)
57
能够在ASP中应用的特性包括了那些由脚本引擎所提供的特性,这意味着VBScript的改进也可在ASP中应用。VBScript的改进如下所述:
58![](/Images/OutliningIndicators/None.gif)
59
1、 在脚本中使用类
60
在VBScript中实现完整的VB类(class)模型,但明显的例外是在ASP服务器端的脚本事件。可以在脚本中创建类,使它们的属性和方法能够和用于页面的其余代码,例如:
61
Class MyClass
62![](/Images/OutliningIndicators/None.gif)
63
Private m_HalfValue ‘Local variable to hold value of HalfValue
64![](/Images/OutliningIndicators/None.gif)
65
Public Property Let HalfValue(vData) ‘executed to set the HalfValue property
66
If vData > 0 Then m_HalfValue = vData
67
End Property
68![](/Images/OutliningIndicators/None.gif)
69
Public Property Get HalfValue() ‘executed to return the HalfValue property
70
HalfValue = m_HalfValue
71
End Property
72![](/Images/OutliningIndicators/None.gif)
73
Public Function GetResult() ‘implements the GetResult method
74
GetResult = m_HalfVaue * 2
75
End Function
76
End Class
77![](/Images/OutliningIndicators/None.gif)
78
Set ObjThis = New MyClass
79![](/Images/OutliningIndicators/None.gif)
80
ObjThis.HalfValue = 21
81![](/Images/OutliningIndicators/None.gif)
82
Response.Write “Value of HalfValue property is “ & objThis.HalfValue & “<BR>”
83
Response.Write “Result of GetResult method is “ & objThis.GetResult & “<BR>”
84
…
85
这段代码产生如下结果:
86
Value of HalfValue property is 21
87
Result of GetResult method is 42
88![](/Images/OutliningIndicators/None.gif)
89
2、 With结构
90
VBScript 5.0支持With结构,使访问一个对象的几个属性或方法的代码更加紧凑:
91
…
92
Set objThis = Server.CreateObject(“This.object”)
93![](/Images/OutliningIndicators/None.gif)
94
With objThis
95
.Property1 = “This value”
96
.Property2 = “Another value”
97
TheResult = .SomeMethod
98
End With
99
…
100![](/Images/OutliningIndicators/None.gif)
101
3、 字符串求值
102
Eval函数(过去只在JavaScript和Jscript中可用)目前在VBScript 5.0中已经得到了支持。允许创建包含脚本代码的字符串,值可为True或False,并在执行后可得到一个结果:
103
…
104
datYourBirthday = Request.Form(“Birthday”)
105
strScript = “datYourBirthday = Date()”
106![](/Images/OutliningIndicators/None.gif)
107
If Eval(strScript) Then
108
Response.write “Happy Brithday!”
109
Else
110
Response.write “Have a nice day!”
111
End If
112
…
113![](/Images/OutliningIndicators/None.gif)
114
4、 语句执行
115
新的Execute函数允许执行一个字符串中的脚本代码,执行方式与Eval函数相同,但是不返回结果。它可以用来动态创建代码中稍后执行的过程,例如:
116
…
117
strCheckBirthday = “Sub CheckBirthday(datYourBirthday)” & vbCrlf_
118
& “ If Eval(datYourBirthday = Date()) Then” & vbCrlf_
119
& “ Response.Write “”Happy Birthday!””” & vbCrlf_
120
&” Else” & vbCrlf_
121
&” Response.write “”Have a nice day!””” & vbCrlf_
122
&” End If” & vbCrlf_
123
&”End Sub” & vbCrlf
124
Execute strCheckBirthday
125
CheckBirthday(Date())
126
…
127
一个回车返回(如程序中示)或冒号字符“:”可用来分隔一个字符串中的各条语句。
128![](/Images/OutliningIndicators/None.gif)
129
5、 设置地区
130
新的SetLocale方法可以用来改变脚本引擎的当前地区,可正确显示特殊的地区特定字符,如带重音符的字符或来自不同字符集的字符。
131
StrCurrentLocale = GetLocale
132
SetLocale(“en-gb”)
133![](/Images/OutliningIndicators/None.gif)
134
6、 正则表达式
135
VBScript 5.0现在支持正则表达式(过去只在JavaScript、Jscript和其他语言中可用)。RegExp对象常用来创建和执行正则表达式,例如:
136
StrTarget = “test testing tested attest late start”
137
Set objRegExp = New RegExp ‘create a regular expression
138![](/Images/OutliningIndicators/None.gif)
139
ObjRegExp.Pattern = “test*” ‘set the search pattern
140
ObjRegExp.IgnoreCase = False ‘set the case sensitivity
141
ObjRegExp.Global = True ‘set the scope
142![](/Images/OutliningIndicators/None.gif)
143
Set colMatches = objRegExp.Execute(strTarget) ‘execute the search
144![](/Images/OutliningIndicators/None.gif)
145
For Each Match in colMatches ‘iterate the colMatches collection
146
Response.Write “Match found at position” & Match.FirstIndex & “.”
147
Resposne.Write “Matched value is ‘” & Match.Value & “’.<BR>”
148
Next
149
执行结果如下:
150
Match found at position 0. Matched value is ‘test’.
151
Match found at position 5. Matched value is ‘test’.
152
Match found at position 13. Matched value is ‘test’;
153
Match found at position 22. Matched value is ‘test’.
154![](/Images/OutliningIndicators/None.gif)
155
7、 在客户端VBScript中设置事件处理程序
156
这不是直接应用于ASP的脚本技术,这个新的特性在编写客户端的VBScript时是很有用的。现在可以动态指定一个函数或子程序与一个事件相关联。例如,假设一个函数的名称为MyFunction(),可把这指定给按钮的OnClick事件:
157
Function MyFunction()
158
…
159
Function implementation code here
160
…
161
End Function
162
…
163
Set objCimButton = document.all(“cmdButton”)
164
Set objCmdButton.OnClick = GetRef(“Myfunction”)
165
这提供了JavaScript和Jscript中的类似功能,函数可以被动态地指定为一个对象的属性。
166![](/Images/OutliningIndicators/None.gif)
167
8、 VBScript中的On Error Goto 0
168
尽管这个技术早先没有被文档记载,但在现有的VBScript版本中能够使用(有着VB背景并且有好奇心的人可能早已发现这个秘密)。它现在已被记录在文档中,并且在执行On Error Resume Next后能够用来“关闭”页面中的定制错误处理。结果是任何后来的错误将引发一个浏览器级或服务器级的错误及相应的对话框/响应。
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
![](/Images/dot.gif)
![](/Images/dot.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/None.gif)
25
![](/Images/OutliningIndicators/None.gif)
26
![](/Images/OutliningIndicators/None.gif)
27
![](/Images/OutliningIndicators/None.gif)
28
![](/Images/OutliningIndicators/None.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)
41
![](/Images/OutliningIndicators/None.gif)
42
![](/Images/OutliningIndicators/None.gif)
43
![](/Images/OutliningIndicators/None.gif)
44
![](/Images/OutliningIndicators/None.gif)
45
![](/Images/OutliningIndicators/None.gif)
46
![](/Images/OutliningIndicators/None.gif)
47
![](/Images/OutliningIndicators/None.gif)
48
![](/Images/OutliningIndicators/None.gif)
49
![](/Images/OutliningIndicators/None.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/None.gif)
52
![](/Images/OutliningIndicators/None.gif)
53
![](/Images/OutliningIndicators/None.gif)
54
![](/Images/OutliningIndicators/None.gif)
55
![](/Images/OutliningIndicators/None.gif)
56
![](/Images/OutliningIndicators/None.gif)
57
![](/Images/OutliningIndicators/None.gif)
58
![](/Images/OutliningIndicators/None.gif)
59
![](/Images/OutliningIndicators/None.gif)
60
![](/Images/OutliningIndicators/None.gif)
61
![](/Images/OutliningIndicators/None.gif)
62
![](/Images/OutliningIndicators/None.gif)
63
![](/Images/OutliningIndicators/None.gif)
64
![](/Images/OutliningIndicators/None.gif)
65
![](/Images/OutliningIndicators/None.gif)
66
![](/Images/OutliningIndicators/None.gif)
67
![](/Images/OutliningIndicators/None.gif)
68
![](/Images/OutliningIndicators/None.gif)
69
![](/Images/OutliningIndicators/None.gif)
70
![](/Images/OutliningIndicators/None.gif)
71
![](/Images/OutliningIndicators/None.gif)
72
![](/Images/OutliningIndicators/None.gif)
73
![](/Images/OutliningIndicators/None.gif)
74
![](/Images/OutliningIndicators/None.gif)
75
![](/Images/OutliningIndicators/None.gif)
76
![](/Images/OutliningIndicators/None.gif)
77
![](/Images/OutliningIndicators/None.gif)
78
![](/Images/OutliningIndicators/None.gif)
79
![](/Images/OutliningIndicators/None.gif)
80
![](/Images/OutliningIndicators/None.gif)
81
![](/Images/OutliningIndicators/None.gif)
82
![](/Images/OutliningIndicators/None.gif)
83
![](/Images/OutliningIndicators/None.gif)
84
![](/Images/OutliningIndicators/None.gif)
85
![](/Images/OutliningIndicators/None.gif)
86
![](/Images/OutliningIndicators/None.gif)
87
![](/Images/OutliningIndicators/None.gif)
88
![](/Images/OutliningIndicators/None.gif)
89
![](/Images/OutliningIndicators/None.gif)
90
![](/Images/OutliningIndicators/None.gif)
91
![](/Images/OutliningIndicators/None.gif)
92
![](/Images/OutliningIndicators/None.gif)
93
![](/Images/OutliningIndicators/None.gif)
94
![](/Images/OutliningIndicators/None.gif)
95
![](/Images/OutliningIndicators/None.gif)
96
![](/Images/OutliningIndicators/None.gif)
97
![](/Images/OutliningIndicators/None.gif)
98
![](/Images/OutliningIndicators/None.gif)
99
![](/Images/OutliningIndicators/None.gif)
100
![](/Images/OutliningIndicators/None.gif)
101
![](/Images/OutliningIndicators/None.gif)
102
![](/Images/OutliningIndicators/None.gif)
103
![](/Images/OutliningIndicators/None.gif)
104
![](/Images/OutliningIndicators/None.gif)
105
![](/Images/OutliningIndicators/None.gif)
106
![](/Images/OutliningIndicators/None.gif)
107
![](/Images/OutliningIndicators/None.gif)
108
![](/Images/OutliningIndicators/None.gif)
109
![](/Images/OutliningIndicators/None.gif)
110
![](/Images/OutliningIndicators/None.gif)
111
![](/Images/OutliningIndicators/None.gif)
112
![](/Images/OutliningIndicators/None.gif)
113
![](/Images/OutliningIndicators/None.gif)
114
![](/Images/OutliningIndicators/None.gif)
115
![](/Images/OutliningIndicators/None.gif)
116
![](/Images/OutliningIndicators/None.gif)
117
![](/Images/OutliningIndicators/None.gif)
118
![](/Images/OutliningIndicators/None.gif)
119
![](/Images/OutliningIndicators/None.gif)
120
![](/Images/OutliningIndicators/None.gif)
121
![](/Images/OutliningIndicators/None.gif)
122
![](/Images/OutliningIndicators/None.gif)
123
![](/Images/OutliningIndicators/None.gif)
124
![](/Images/OutliningIndicators/None.gif)
125
![](/Images/OutliningIndicators/None.gif)
126
![](/Images/OutliningIndicators/None.gif)
127
![](/Images/OutliningIndicators/None.gif)
128
![](/Images/OutliningIndicators/None.gif)
129
![](/Images/OutliningIndicators/None.gif)
130
![](/Images/OutliningIndicators/None.gif)
131
![](/Images/OutliningIndicators/None.gif)
132
![](/Images/OutliningIndicators/None.gif)
133
![](/Images/OutliningIndicators/None.gif)
134
![](/Images/OutliningIndicators/None.gif)
135
![](/Images/OutliningIndicators/None.gif)
136
![](/Images/OutliningIndicators/None.gif)
137
![](/Images/OutliningIndicators/None.gif)
138
![](/Images/OutliningIndicators/None.gif)
139
![](/Images/OutliningIndicators/None.gif)
140
![](/Images/OutliningIndicators/None.gif)
141
![](/Images/OutliningIndicators/None.gif)
142
![](/Images/OutliningIndicators/None.gif)
143
![](/Images/OutliningIndicators/None.gif)
144
![](/Images/OutliningIndicators/None.gif)
145
![](/Images/OutliningIndicators/None.gif)
146
![](/Images/OutliningIndicators/None.gif)
147
![](/Images/OutliningIndicators/None.gif)
148
![](/Images/OutliningIndicators/None.gif)
149
![](/Images/OutliningIndicators/None.gif)
150
![](/Images/OutliningIndicators/None.gif)
151
![](/Images/OutliningIndicators/None.gif)
152
![](/Images/OutliningIndicators/None.gif)
153
![](/Images/OutliningIndicators/None.gif)
154
![](/Images/OutliningIndicators/None.gif)
155
![](/Images/OutliningIndicators/None.gif)
156
![](/Images/OutliningIndicators/None.gif)
157
![](/Images/OutliningIndicators/None.gif)
158
![](/Images/OutliningIndicators/None.gif)
159
![](/Images/OutliningIndicators/None.gif)
160
![](/Images/OutliningIndicators/None.gif)
161
![](/Images/OutliningIndicators/None.gif)
162
![](/Images/OutliningIndicators/None.gif)
163
![](/Images/OutliningIndicators/None.gif)
164
![](/Images/OutliningIndicators/None.gif)
165
![](/Images/OutliningIndicators/None.gif)
166
![](/Images/OutliningIndicators/None.gif)
167
![](/Images/OutliningIndicators/None.gif)
168
![](/Images/OutliningIndicators/None.gif)
posted on 2005-06-05 22:30 ξσ Dicky σξ 阅读(798) 评论(0) 编辑 收藏 举报