1
在ASP中使用类(class)
2
在不入前的一天,当我为了解决一个语法问题来翻阅VBscript文档时,偶然间发现在了下面的一句话:
3
4
Class Statement
5
6
Declares the name of a class, as well as a definition of the variables, properties, and methods that comprise the class.
7
8
翻译过来就是

9
10
Class 声明
11
12
声明一个类的名字,就是定义一些变量,属性,方法来组成一个类
13
14
这是真的!!!?VBScript中能用类!?!?不知道能不能用于ASP!?这样的话,我就不是能写出像object一样的ASP程序?!说干就干!实践是检验真理的唯一标准,自个动手吧!
15
16
我们常常看到别的程序语言中中都有类的说明,PHP,VB,C++,这个在VBScript中的类的说明,我是第一次听到,我们的日常工作就是网站开发,在这个里面多多少少搞出点经验,像模像样也能自诩为"内行",所以我就来分享一下我所知道的这个新的东东。我们来看看下面的这个代码吧!(window2000+IIS5.0通过测试)
17
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
35
End Class
36
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
42
%>
43
44
45
这是很简单的一个程序,我们在其中声明了一个名为aspcn的类,建立了一个DoIt函数,一个QueryStr方法,这个程序很简单相信大家能看懂,它的显示如下:
46
47
Aspcn Is Good!
48
Aspcn Is Cool!
49
http://www.aspcn.com/
50
WelCome!!!
51
52
以后,咱们就可以把我们常用到的程序写成一个类,到时候就用<!--#include file="xxx.asp"-->来包含进来就行了,这给我们开发程序又提供了新的空间,真是爽啊!和C++一样了,有点回归自然的感觉.
53
54
55
VBSctipt 5.0中的新特性
56
57
能够在ASP中应用的特性包括了那些由脚本引擎所提供的特性,这意味着VBScript的改进也可在ASP中应用。VBScript的改进如下所述:
58
59
1、 在脚本中使用类
60
在VBScript中实现完整的VB类(class)模型,但明显的例外是在ASP服务器端的脚本事件。可以在脚本中创建类,使它们的属性和方法能够和用于页面的其余代码,例如:
61
Class MyClass
62
63
Private m_HalfValue ‘Local variable to hold value of HalfValue
64
65
Public Property Let HalfValue(vData) ‘executed to set the HalfValue property
66
If vData > 0 Then m_HalfValue = vData
67
End Property
68
69
Public Property Get HalfValue() ‘executed to return the HalfValue property
70
HalfValue = m_HalfValue
71
End Property
72
73
Public Function GetResult() ‘implements the GetResult method
74
GetResult = m_HalfVaue * 2
75
End Function
76
End Class
77
78
Set ObjThis = New MyClass
79
80
ObjThis.HalfValue = 21
81
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
89
2、 With结构
90
VBScript 5.0支持With结构,使访问一个对象的几个属性或方法的代码更加紧凑:
91
…
92
Set objThis = Server.CreateObject(“This.object”)
93
94
With objThis
95
.Property1 = “This value”
96
.Property2 = “Another value”
97
TheResult = .SomeMethod
98
End With
99
…
100
101
3、 字符串求值
102
Eval函数(过去只在JavaScript和Jscript中可用)目前在VBScript 5.0中已经得到了支持。允许创建包含脚本代码的字符串,值可为True或False,并在执行后可得到一个结果:
103
…
104
datYourBirthday = Request.Form(“Birthday”)
105
strScript = “datYourBirthday = Date()”
106
107
If Eval(strScript) Then
108
Response.write “Happy Brithday!”
109
Else
110
Response.write “Have a nice day!”
111
End If
112
…
113
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
129
5、 设置地区
130
新的SetLocale方法可以用来改变脚本引擎的当前地区,可正确显示特殊的地区特定字符,如带重音符的字符或来自不同字符集的字符。
131
StrCurrentLocale = GetLocale
132
SetLocale(“en-gb”)
133
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
139
ObjRegExp.Pattern = “test*” ‘set the search pattern
140
ObjRegExp.IgnoreCase = False ‘set the case sensitivity
141
ObjRegExp.Global = True ‘set the scope
142
143
Set colMatches = objRegExp.Execute(strTarget) ‘execute the search
144
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
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
167
8、 VBScript中的On Error Goto 0
168
尽管这个技术早先没有被文档记载,但在现有的VBScript版本中能够使用(有着VB背景并且有好奇心的人可能早已发现这个秘密)。它现在已被记录在文档中,并且在执行On Error Resume Next后能够用来“关闭”页面中的定制错误处理。结果是任何后来的错误将引发一个浏览器级或服务器级的错误及相应的对话框/响应。

2

3

4

5

6

7

8



9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168
