天天看点

在ASP中使用类(Class)

  1

在ASP中使用类(Class)

在ASP中使用类(class)

  2

在ASP中使用类(Class)

在不入前的一天,当我为了解决一个语法问题来翻阅VBscript文档时,偶然间发现在了下面的一句话:

  3

在ASP中使用类(Class)

  4

在ASP中使用类(Class)

Class Statement

  5

在ASP中使用类(Class)

  6

在ASP中使用类(Class)

Declares the name of a class, 

as

 well 

as

 a definition of the variables, properties, 

and

 methods that comprise the class. 

  7

在ASP中使用类(Class)

  8

在ASP中使用类(Class)

翻译过来就是

在ASP中使用类(Class)
在ASP中使用类(Class)

  9

在ASP中使用类(Class)

 10

在ASP中使用类(Class)

Class 声明

 11

在ASP中使用类(Class)

 12

在ASP中使用类(Class)

声明一个类的名字,就是定义一些变量,属性,方法来组成一个类 

 13

在ASP中使用类(Class)

 14

在ASP中使用类(Class)

这是真的!!!?VBScript中能用类!?!?不知道能不能用于ASP!?这样的话,我就不是能写出像object一样的ASP程序?!说干就干!实践是检验真理的唯一标准,自个动手吧!

 15

在ASP中使用类(Class)

 16

在ASP中使用类(Class)

我们常常看到别的程序语言中中都有类的说明,PHP,VB,C

++

,这个在VBScript中的类的说明,我是第一次听到,我们的日常工作就是网站开发,在这个里面多多少少搞出点经验,像模像样也能自诩为

"

内行

"

,所以我就来分享一下我所知道的这个新的东东。我们来看看下面的这个代码吧!(window2000

+

IIS5.0通过测试)

 17

在ASP中使用类(Class)

 18

在ASP中使用类(Class)

<

%

 19

在ASP中使用类(Class)

'

'声明一个名为aspcn的类 

 20

在ASP中使用类(Class)

Class aspcn

 21

在ASP中使用类(Class)

Private

 aspcn

 22

在ASP中使用类(Class)

'

'初始化类

 23

在ASP中使用类(Class)

Private

Sub

 Class_Initialize

 24

在ASP中使用类(Class)

aspcn

=

"

Aspcn Is Good!<br>"

 25

在ASP中使用类(Class)

End Sub

 26

在ASP中使用类(Class)

'

'定义一个函数

 27

在ASP中使用类(Class)

Public

Function

 DoIt()

 28

在ASP中使用类(Class)

DoIt

=

aspcn

 29

在ASP中使用类(Class)

End Function

 30

在ASP中使用类(Class)

'

'定义一个方法

 31

在ASP中使用类(Class)

Public

Sub

 QueryStr(stat)

 32

在ASP中使用类(Class)

Response.write stat

 33

在ASP中使用类(Class)

End Sub

 34

在ASP中使用类(Class)

 35

在ASP中使用类(Class)

End

 Class

 36

在ASP中使用类(Class)

 37

在ASP中使用类(Class)

Set

 Hi_aspcn

=

New

 aspcn 

'

'定义一个名为Hi_aspcn的aspcn对象实例

 38

在ASP中使用类(Class)

response.write Hi_aspcn.DoIt

 39

在ASP中使用类(Class)

varstr

=

"

Aspcn Is Cool!<br><font color=red>http://www.aspcn.com</font><br>WelCome!!!"

 40

在ASP中使用类(Class)

Hi_aspcn.QueryStr varstr 

 41

在ASP中使用类(Class)

 42

在ASP中使用类(Class)

%

>

 43

在ASP中使用类(Class)

 44

在ASP中使用类(Class)

 45

在ASP中使用类(Class)

这是很简单的一个程序,我们在其中声明了一个名为aspcn的类,建立了一个DoIt函数,一个QueryStr方法,这个程序很简单相信大家能看懂,它的显示如下: 

 46

在ASP中使用类(Class)

 47

在ASP中使用类(Class)

Aspcn 

Is

 Good!

 48

在ASP中使用类(Class)

Aspcn 

Is

 Cool!

 49

在ASP中使用类(Class)

http:

//

www.aspcn.com

/

 50

在ASP中使用类(Class)

WelCome!!! 

 51

在ASP中使用类(Class)

 52

在ASP中使用类(Class)

以后,咱们就可以把我们常用到的程序写成一个类,到时候就用

<

--

#include file

=

"

xxx.asp

"

-->

来包含进来就行了,这给我们开发程序又提供了新的空间,真是爽啊!和C

++

一样了,有点回归自然的感觉. 

 53

在ASP中使用类(Class)

 54

在ASP中使用类(Class)

 55

在ASP中使用类(Class)

     VBSctipt 

5

.0中的新特性 

 56

在ASP中使用类(Class)

 57

在ASP中使用类(Class)

能够在ASP中应用的特性包括了那些由脚本引擎所提供的特性,这意味着VBScript的改进也可在ASP中应用。VBScript的改进如下所述:

 58

在ASP中使用类(Class)

 59

在ASP中使用类(Class)

1

、 在脚本中使用类

 60

在ASP中使用类(Class)

在VBScript中实现完整的VB类(class)模型,但明显的例外是在ASP服务器端的脚本事件。可以在脚本中创建类,使它们的属性和方法能够和用于页面的其余代码,例如:

 61

在ASP中使用类(Class)

Class MyClass

 62

在ASP中使用类(Class)

 63

在ASP中使用类(Class)

Private

 m_HalfValue ‘

Local

 variable 

to

 hold value of HalfValue

 64

在ASP中使用类(Class)

 65

在ASP中使用类(Class)

Public

Property

Let

 HalfValue(vData) ‘executed 

to

set

 the HalfValue 

property

 66

在ASP中使用类(Class)

If

 vData 

>

Then

 m_HalfValue 

=

 vData

 67

在ASP中使用类(Class)

End Property

 68

在ASP中使用类(Class)

 69

在ASP中使用类(Class)

Public

Property

Get

 HalfValue() ‘executed 

to

 return the HalfValue 

property

 70

在ASP中使用类(Class)

HalfValue 

=

 m_HalfValue

 71

在ASP中使用类(Class)

End Property

 72

在ASP中使用类(Class)

 73

在ASP中使用类(Class)

Public

Function

 GetResult() ‘implements the GetResult method

 74

在ASP中使用类(Class)

GetResult 

=

 m_HalfVaue 

*

2

 75

在ASP中使用类(Class)

End Function

 76

在ASP中使用类(Class)

End

 Class

 77

在ASP中使用类(Class)

 78

在ASP中使用类(Class)

Set

 ObjThis 

=

New

 MyClass

 79

在ASP中使用类(Class)

 80

在ASP中使用类(Class)

ObjThis.HalfValue 

=

21

 81

在ASP中使用类(Class)

 82

在ASP中使用类(Class)

Response.Write “Value of HalfValue 

property

is

 “ 

&

 objThis.HalfValue 

&

 “

<

BR

>

 83

在ASP中使用类(Class)

Response.Write “Result of GetResult method 

is

 “ 

&

 objThis.GetResult 

&

 “

<

BR

>

 84

在ASP中使用类(Class)

 85

在ASP中使用类(Class)

这段代码产生如下结果:

 86

在ASP中使用类(Class)

Value of HalfValue 

property

is

21

 87

在ASP中使用类(Class)

Result of GetResult method 

is

42

 88

在ASP中使用类(Class)

 89

在ASP中使用类(Class)

2

、 With结构

 90

在ASP中使用类(Class)

VBScript 

5

.0支持With结构,使访问一个对象的几个属性或方法的代码更加紧凑:

 91

在ASP中使用类(Class)

 92

在ASP中使用类(Class)

Set

 objThis 

=

 Server.

CreateObject

(“This.

object

”)

 93

在ASP中使用类(Class)

 94

在ASP中使用类(Class)

With

 objThis

 95

在ASP中使用类(Class)

.Property1 

=

 “This value”

 96

在ASP中使用类(Class)

.Property2 

=

 “Another value”

 97

在ASP中使用类(Class)

TheResult 

=

 .SomeMethod

 98

在ASP中使用类(Class)

End

With

 99

在ASP中使用类(Class)

100

在ASP中使用类(Class)

101

在ASP中使用类(Class)

3

、 字符串求值

102

在ASP中使用类(Class)

Eval函数(过去只在JavaScript和Jscript中可用)目前在VBScript 

5

.0中已经得到了支持。允许创建包含脚本代码的字符串,值可为True或False,并在执行后可得到一个结果:

103

在ASP中使用类(Class)

104

在ASP中使用类(Class)

datYourBirthday 

=

 Request.Form(“Birthday”)

105

在ASP中使用类(Class)

strScript 

=

 “datYourBirthday 

=

Date

()”

106

在ASP中使用类(Class)

107

在ASP中使用类(Class)

If

Eval

(strScript) 

Then

108

在ASP中使用类(Class)

Response.write “Happy Brithday!”

109

在ASP中使用类(Class)

Else

110

在ASP中使用类(Class)

Response.write “Have a nice 

day

!”

111

在ASP中使用类(Class)

End

If

112

在ASP中使用类(Class)

113

在ASP中使用类(Class)

114

在ASP中使用类(Class)

4

、 语句执行

115

在ASP中使用类(Class)

新的Execute函数允许执行一个字符串中的脚本代码,执行方式与Eval函数相同,但是不返回结果。它可以用来动态创建代码中稍后执行的过程,例如:

116

在ASP中使用类(Class)

117

在ASP中使用类(Class)

strCheckBirthday 

=

 “

Sub

 CheckBirthday(datYourBirthday)” 

&

 vbCrlf_

118

在ASP中使用类(Class)

&

 “ 

If

Eval

(datYourBirthday 

=

Date

()) 

Then

” 

&

 vbCrlf_

119

在ASP中使用类(Class)

&

 “ Response.Write “”Happy Birthday!””” 

&

 vbCrlf_

120

在ASP中使用类(Class)

&

” 

Else

” 

&

 vbCrlf_

121

在ASP中使用类(Class)

&

” Response.write “”Have a nice 

day

!””” 

&

 vbCrlf_

122

在ASP中使用类(Class)

&

” 

End

If

” 

&

 vbCrlf_

123

在ASP中使用类(Class)

&

End Sub

” 

&

 vbCrlf

124

在ASP中使用类(Class)

Execute

 strCheckBirthday

125

在ASP中使用类(Class)

CheckBirthday(

Date

())

126

在ASP中使用类(Class)

127

在ASP中使用类(Class)

一个回车返回(如程序中示)或冒号字符“:”可用来分隔一个字符串中的各条语句。

128

在ASP中使用类(Class)

129

在ASP中使用类(Class)

5

、 设置地区

130

在ASP中使用类(Class)

新的SetLocale方法可以用来改变脚本引擎的当前地区,可正确显示特殊的地区特定字符,如带重音符的字符或来自不同字符集的字符。

131

在ASP中使用类(Class)

StrCurrentLocale 

=

GetLocale

132

在ASP中使用类(Class)

SetLocale

(“en

-

gb”)

133

在ASP中使用类(Class)

134

在ASP中使用类(Class)

6

、 正则表达式

135

在ASP中使用类(Class)

VBScript 

5

.0现在支持正则表达式(过去只在JavaScript、Jscript和其他语言中可用)。RegExp对象常用来创建和执行正则表达式,例如:

136

在ASP中使用类(Class)

StrTarget 

=

 “test testing tested attest late start”

137

在ASP中使用类(Class)

Set

 objRegExp 

=

New

 RegExp ‘create a regular expression

138

在ASP中使用类(Class)

139

在ASP中使用类(Class)

ObjRegExp.Pattern 

=

 “test

*

” ‘

set

 the search pattern

140

在ASP中使用类(Class)

ObjRegExp.IgnoreCase 

=

False

 ‘

set

 the 

case

 sensitivity

141

在ASP中使用类(Class)

ObjRegExp.Global 

=

True

 ‘

set

 the scope

142

在ASP中使用类(Class)

143

在ASP中使用类(Class)

Set

 colMatches 

=

 objRegExp.

Execute

(strTarget) ‘

execute

 the search

144

在ASP中使用类(Class)

145

在ASP中使用类(Class)

For

Each

 Match in colMatches ‘iterate the colMatches collection

146

在ASP中使用类(Class)

Response.Write “Match found at position” 

&

 Match.FirstIndex 

&

 “.”

147

在ASP中使用类(Class)

Resposne.Write “Matched value 

is

 ‘” 

&

 Match.Value 

&

 “’.

<

BR

>

148

在ASP中使用类(Class)

Next

149

在ASP中使用类(Class)

执行结果如下:

150

在ASP中使用类(Class)

Match found at position 

. Matched value 

is

 ‘test’.

151

在ASP中使用类(Class)

Match found at position 

5

. Matched value 

is

 ‘test’.

152

在ASP中使用类(Class)

Match found at position 

13

. Matched value 

is

 ‘test’;

153

在ASP中使用类(Class)

Match found at position 

22

. Matched value 

is

 ‘test’.

154

在ASP中使用类(Class)

155

在ASP中使用类(Class)

7

、 在客户端VBScript中设置事件处理程序

156

在ASP中使用类(Class)

这不是直接应用于ASP的脚本技术,这个新的特性在编写客户端的VBScript时是很有用的。现在可以动态指定一个函数或子程序与一个事件相关联。例如,假设一个函数的名称为MyFunction(),可把这指定给按钮的OnClick事件:

157

在ASP中使用类(Class)

Function

 MyFunction()

158

在ASP中使用类(Class)

159

在ASP中使用类(Class)

Function

 implementation code here

160

在ASP中使用类(Class)

161

在ASP中使用类(Class)

End Function

162

在ASP中使用类(Class)

163

在ASP中使用类(Class)

Set

 objCimButton 

=

 document.all(“cmdButton”)

164

在ASP中使用类(Class)

Set

 objCmdButton.OnClick 

=

GetRef

(“Myfunction”)

165

在ASP中使用类(Class)

这提供了JavaScript和Jscript中的类似功能,函数可以被动态地指定为一个对象的属性。

166

在ASP中使用类(Class)

167

在ASP中使用类(Class)

8

、 VBScript中的On 

Error

Goto

168

继续阅读