主题:[转贴]在你的ASP中使用类(class)
圪圪
[专家分:5150] 发布于 2004-07-13 09:04:00
在不入前的一天,当我为了解决一个语法问题来翻阅VBscript文档时,偶然间发现在了下面的一句话:
Class Statement
Declares the name of a class, as well as a definition of the variables, properties, and methods that comprise the class.
翻译过来就是......
Class 声明
声明一个类的名字,就是定义一些变量,属性,方法来组成一个类
这是真的!!!?VBScript中能用类!?!?不知道能不能用于ASP!?这样的话,我就不是能写出像object一样的ASP程序?!说干就干!实践是检验真理的唯一标准,自个动手吧!
我们常常看到别的程序语言中中都有类的说明,PHP,VB,C++,这个在VBScript中的类的说明,我是第一次听到,我们的日常工作就是网站开发,在这个里面多多少少搞出点经验,像模像样也能自诩为"内行",所以我就来分享一下我所知道的这个新的东东。我们来看看下面的这个代码吧!(window2000+IIS5.0通过测试)
<%
''声明一个名为aspcn的类
Class aspcn
Private aspcn
''初始化类
Private Sub Class_Initialize
aspcn="Aspcn Is Good!<br>"
End Sub
''定义一个函数
Public Function DoIt()
DoIt=aspcn
End Function
''定义一个方法
Public Sub QueryStr(stat)
Response.write stat
End Sub
End Class
Set Hi_aspcn=New aspcn ''定义一个名为Hi_aspcn的aspcn对象实例
response.write Hi_aspcn.DoIt
varstr="Aspcn Is Cool!<br><font color=red>http://www.aspcn.com</font><br>WelCome!!!"
Hi_aspcn.QueryStr varstr
%>
这是很简单的一个程序,我们在其中声明了一个名为aspcn的类,建立了一个DoIt函数,一个QueryStr方法,这个程序很简单相信大家能看懂,它的显示如下:
Aspcn Is Good!
Aspcn Is Cool!
http://www.aspcn.com
WelCome!!!
以后,咱们就可以把我们常用到的程序写成一个类,到时候就用<!--#include file="xxx.asp"-->来包含进来就行了,这给我们开发程序又提供了新的空间,真是爽啊!和C++一样了,有点回归自然的感觉.
回复列表 (共10个回复)
沙发
圪圪 [专家分:5150] 发布于 2004-02-16 18:01:00
VBSctipt 5.0中的新特性
能够在ASP中应用的特性包括了那些由脚本引擎所提供的特性,这意味着VBScript的改进也可在ASP中应用。VBScript的改进如下所述:
1、 在脚本中使用类
在VBScript中实现完整的VB类(class)模型,但明显的例外是在ASP服务器端的脚本事件。可以在脚本中创建类,使它们的属性和方法能够和用于页面的其余代码,例如:
Class MyClass
Private m_HalfValue ‘Local variable to hold value of HalfValue
Public Property Let HalfValue(vData) ‘executed to set the HalfValue property
If vData > 0 Then m_HalfValue = vData
End Property
Public Property Get HalfValue() ‘executed to return the HalfValue property
HalfValue = m_HalfValue
End Property
Public Function GetResult() ‘implements the GetResult method
GetResult = m_HalfVaue * 2
End Function
End Class
Set ObjThis = New MyClass
ObjThis.HalfValue = 21
Response.Write “Value of HalfValue property is “ & objThis.HalfValue & “<BR>”
Response.Write “Result of GetResult method is “ & objThis.GetResult & “<BR>”
…
这段代码产生如下结果:
Value of HalfValue property is 21
Result of GetResult method is 42
2、 With结构
VBScript 5.0支持With结构,使访问一个对象的几个属性或方法的代码更加紧凑:
…
Set objThis = Server.CreateObject(“This.object”)
With objThis
.Property1 = “This value”
.Property2 = “Another value”
TheResult = .SomeMethod
End With
…
3、 字符串求值
Eval函数(过去只在JavaScript和Jscript中可用)目前在VBScript 5.0中已经得到了支持。允许创建包含脚本代码的字符串,值可为True或False,并在执行后可得到一个结果:
…
datYourBirthday = Request.Form(“Birthday”)
strScript = “datYourBirthday = Date()”
If Eval(strScript) Then
Response.write “Happy Brithday!”
Else
Response.write “Have a nice day!”
End If
…
4、 语句执行
新的Execute函数允许执行一个字符串中的脚本代码,执行方式与Eval函数相同,但是不返回结果。它可以用来动态创建代码中稍后执行的过程,例如:
…
strCheckBirthday = “Sub CheckBirthday(datYourBirthday)” & vbCrlf_
& “ If Eval(datYourBirthday = Date()) Then” & vbCrlf_
& “ Response.Write “”Happy Birthday!””” & vbCrlf_
&” Else” & vbCrlf_
&” Response.write “”Have a nice day!””” & vbCrlf_
&” End If” & vbCrlf_
&”End Sub” & vbCrlf
Execute strCheckBirthday
CheckBirthday(Date())
…
一个回车返回(如程序中示)或冒号字符“:”可用来分隔一个字符串中的各条语句。
5、 设置地区
新的SetLocale方法可以用来改变脚本引擎的当前地区,可正确显示特殊的地区特定字符,如带重音符的字符或来自不同字符集的字符。
StrCurrentLocale = GetLocale
SetLocale(“en-gb”)
6、 正则表达式
VBScript 5.0现在支持正则表达式(过去只在JavaScript、Jscript和其他语言中可用)。RegExp对象常用来创建和执行正则表达式,例如:
StrTarget = “test testing tested attest late start”
Set objRegExp = New RegExp ‘create a regular expression
ObjRegExp.Pattern = “test*” ‘set the search pattern
ObjRegExp.IgnoreCase = False ‘set the case sensitivity
ObjRegExp.Global = True ‘set the scope
Set colMatches = objRegExp.Execute(strTarget) ‘execute the search
For Each Match in colMatches ‘iterate the colMatches collection
Response.Write “Match found at position” & Match.FirstIndex & “.”
Resposne.Write “Matched value is ‘” & Match.Value & “’.<BR>”
Next
执行结果如下:
Match found at position 0. Matched value is ‘test’.
Match found at position 5. Matched value is ‘test’.
Match found at position 13. Matched value is ‘test’;
Match found at position 22. Matched value is ‘test’.
7、 在客户端VBScript中设置事件处理程序
这不是直接应用于ASP的脚本技术,这个新的特性在编写客户端的VBScript时是很有用的。现在可以动态指定一个函数或子程序与一个事件相关联。例如,假设一个函数的名称为MyFunction(),可把这指定给按钮的OnClick事件:
Function MyFunction()
…
Function implementation code here
…
End Function
…
Set objCimButton = document.all(“cmdButton”)
Set objCmdButton.OnClick = GetRef(“Myfunction”)
这提供了JavaScript和Jscript中的类似功能,函数可以被动态地指定为一个对象的属性。
8、 VBScript中的On Error Goto 0
尽管这个技术早先没有被文档记载,但在现有的VBScript版本中能够使用(有着VB背景并且有好奇心的人可能早已发现这个秘密)。它现在已被记录在文档中,并且在执行On Error Resume Next后能够用来“关闭”页面中的定制错误处理。结果是任何后来的错误将引发一个浏览器级或服务器级的错误及相应的对话框/响应。
板凳
圪圪 [专家分:5150] 发布于 2004-02-16 18:08:00
这个类是用于处理字符串的,是老外写的,我把里面的功能和参数加了说明
使用方法:
=============== test.asp================
<!--#include file="StringOperations.asp"-->
<%
dim str
set str = New StringOperations
test = str.toCharArray("check this out")
response.write "<strong>str.toCharArray</strong>: "
for i = 0 to ubound(test)
response.write test(i) & " "
next
response.write "<BR><BR>"
test1 = str.arrayToString(test)
response.write "<strong>str.arrayToString</strong>: " & test1
response.write "<BR><BR>"
response.write "<strong>str.startsWith</strong>: " & str.startsWith(test1, "ch")
response.write "<BR><BR>"
response.write "<strong>str.endWith</strong>: " & str.endsWith(test1, "out")
response.write "<BR><BR>"
response.write "<strong>str.clone</strong>: " & str.clone("abc", 10)
response.write "<BR><BR>"
response.write "<strong>str.trimStart</strong>: " & str.trimStart(test1, 3)
response.write "<BR><BR>"
response.write "<strong>str.trimEnd</strong>: " & str.trimEnd(test1, 2)
response.write "<BR><BR>"
response.write "<strong>str.swapCase</strong>: " & str.swapCase("HiHiHi")
response.write "<BR><BR>"
response.write "<strong>str.isAlphabetic</strong>: " & str.isAlphabetic("!")
response.write "<BR><BR>"
response.write "<strong>str.capitalize</strong>: " & str.capitalize("clara fehler")
Set str = Nothing
%>
=============== StringOperations.asp================
<%
class StringOperations
'****************************************************************************
'' @功能说明: 把字符串换为char型数组
'' @参数说明: - str [string]: 需要转换的字符串
'' @返回值: - [Array] Char型数组
'****************************************************************************
public function toCharArray(byVal str)
redim charArray(len(str))
for i = 1 to len(str)
charArray(i-1) = Mid(str,i,1)
next
toCharArray = charArray
end function
'****************************************************************************
'' @功能说明: 把一个数组转换成一个字符串
'' @参数说明: - arr [Array]: 需要转换的数据
'' @返回值: - [string] 字符串
'****************************************************************************
public function arrayToString(byVal arr)
for i = 0 to UBound(arr)
strObj = strObj & arr(i)
next
arrayToString = strObj
end function
'****************************************************************************
'' @功能说明: 检查源字符串str是否以chars开头
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - chars [string]: 比较的字符/字符串
'' @返回值: - [bool]
'****************************************************************************
public function startsWith(byVal str, chars)
if Left(str,len(chars)) = chars then
startsWith = true
else
startsWith = false
end if
end function
'****************************************************************************
'' @功能说明: 检查源字符串str是否以chars结尾
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - chars [string]: 比较的字符/字符串
'' @返回值: - [bool]
'****************************************************************************
public function endsWith(byVal str, chars)
if Right(str,len(chars)) = chars then
endsWith = true
else
endsWith = false
end if
end function
'****************************************************************************
'' @功能说明: 复制N个字符串str
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - n [int]: 复制次数
'' @返回值: - [string] 复制后的字符串
'****************************************************************************
public function clone(byVal str, n)
for i = 1 to n
value = value & str
next
clone = value
end function
'****************************************************************************
'' @功能说明: 删除源字符串str的前N个字符
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - n [int]: 删除的字符个数
'' @返回值: - [string] 删除后的字符串
'****************************************************************************
public function trimStart(byVal str, n)
value = Mid(str, n+1)
trimStart = value
end function
'****************************************************************************
'' @功能说明: 删除源字符串str的最后N个字符串
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - n [int]: 删除的字符个数
'' @返回值: - [string] 删除后的字符串
'****************************************************************************
public function trimEnd(byVal str, n)
value = Left(str, len(str)-n)
trimEnd = value
end function
'****************************************************************************
'' @功能说明: 检查字符character是否是英文字符 A-Z or a-z
'' @参数说明: - character [char]: 检查的字符
'' @返回值: - [bool] 如果是英文字符,返回TRUE,反之为FALSE
'****************************************************************************
public function isAlphabetic(byVal character)
asciiValue = cint(asc(character))
if (65 <= asciiValue and asciiValue <= 90) or (97 <= asciiValue and asciiValue <= 122) then
isAlphabetic = true
else
isAlphabetic = false
end if
end function
'****************************************************************************
'' @功能说明: 对str字符串进行大小写转换
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function swapCase(str)
for i = 1 to len(str)
current = mid(str, i, 1)
if isAlphabetic(current) then
high = asc(ucase(current))
low = asc(lcase(current))
sum = high + low
return = return & chr(sum-asc(current))
else
return = return & current
end if
next
swapCase = return
end function
'****************************************************************************
'' @功能说明: 将源字符串str中每个单词的第一个字母转换成大写
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function capitalize(str)
words = split(str," ")
for i = 0 to ubound(words)
if not i = 0 then
tmp = " "
end if
tmp = tmp & ucase(left(words(i), 1)) & right(words(i), len(words(i))-1)
words(i) = tmp
next
capitalize = arrayToString(words)
end function
end class
%>
3 楼
圪圪 [专家分:5150] 发布于 2004-02-16 18:09:00
相关文章参见:
http://www.csdn.net/Develop/read_article.asp?id=22695
本文在此基础上进行了一些添加,加了几个适合中文网站的FUNCTION进去,可能还有些没有补充进去,有感兴趣的朋友可以再在此基础上加一点FUNCTION进去,不过可别忘记分享一下!
<%
class StringOperations
'****************************************************************************
'' @功能说明: 把字符串换为char型数组
'' @参数说明: - str [string]: 需要转换的字符串
'' @返回值: - [Array] Char型数组
'****************************************************************************
public function toCharArray(byVal str)
redim charArray(len(str))
for i = 1 to len(str)
charArray(i-1) = Mid(str,i,1)
next
toCharArray = charArray
end function
'****************************************************************************
'' @功能说明: 把一个数组转换成一个字符串
'' @参数说明: - arr [Array]: 需要转换的数据
'' @返回值: - [string] 字符串
'****************************************************************************
public function arrayToString(byVal arr)
for i = 0 to UBound(arr)
strObj = strObj & arr(i)
next
arrayToString = strObj
end function
'****************************************************************************
'' @功能说明: 检查源字符串str是否以chars开头
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - chars [string]: 比较的字符/字符串
'' @返回值: - [bool]
'****************************************************************************
public function startsWith(byVal str, chars)
if Left(str,len(chars)) = chars then
startsWith = true
else
startsWith = false
end if
end function
'****************************************************************************
'' @功能说明: 检查源字符串str是否以chars结尾
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - chars [string]: 比较的字符/字符串
'' @返回值: - [bool]
'****************************************************************************
public function endsWith(byVal str, chars)
if Right(str,len(chars)) = chars then
endsWith = true
else
endsWith = false
end if
end function
'****************************************************************************
'' @功能说明: 复制N个字符串str
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - n [int]: 复制次数
'' @返回值: - [string] 复制后的字符串
'****************************************************************************
public function clone(byVal str, n)
for i = 1 to n
value = value & str
next
clone = value
end function
'****************************************************************************
'' @功能说明: 删除源字符串str的前N个字符
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - n [int]: 删除的字符个数
'' @返回值: - [string] 删除后的字符串
'****************************************************************************
public function trimStart(byVal str, n)
value = Mid(str, n+1)
trimStart = value
end function
'****************************************************************************
'' @功能说明: 删除源字符串str的最后N个字符串
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - n [int]: 删除的字符个数
'' @返回值: - [string] 删除后的字符串
'****************************************************************************
public function trimEnd(byVal str, n)
value = Left(str, len(str)-n)
trimEnd = value
end function
'****************************************************************************
'' @功能说明: 检查字符character是否是英文字符 A-Z or a-z
'' @参数说明: - character [char]: 检查的字符
'' @返回值: - [bool] 如果是英文字符,返回TRUE,反之为FALSE
'****************************************************************************
public function isAlphabetic(byVal character)
asciiValue = cint(asc(character))
if (65 <= asciiValue and asciiValue <= 90) or (97 <= asciiValue and asciiValue <= 122) then
isAlphabetic = true
else
isAlphabetic = false
end if
end function
'****************************************************************************
'' @功能说明: 对str字符串进行大小写转换
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function swapCase(str)
for i = 1 to len(str)
current = mid(str, i, 1)
if isAlphabetic(current) then
high = asc(ucase(current))
low = asc(lcase(current))
sum = high + low
return = return & chr(sum-asc(current))
else
return = return & current
end if
next
swapCase = return
end function
'****************************************************************************
'' @功能说明: 将源字符串str中每个单词的第一个字母转换成大写
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function capitalize(str)
words = split(str," ")
for i = 0 to ubound(words)
if not i = 0 then
tmp = " "
end if
tmp = tmp & ucase(left(words(i), 1)) & right(words(i), len(words(i))-1)
words(i) = tmp
next
capitalize = arrayToString(words)
end function
'****************************************************************************
'' @功能说明: 将源字符Str后中的'过滤为''
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
public function checkstr(Str)
If Trim(Str)="" Or IsNull(str) Then
checkstr=""
else
checkstr=Replace(Trim(Str),"'","''")
end if
End function
'****************************************************************************
'' @功能说明: 将字符串中的str中的HTML代码进行过滤
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [string] 转换后的字符串
'****************************************************************************
Public Function HtmlEncode(str)
If Trim(Str)="" Or IsNull(str) then
HtmlEncode=""
else
str=Replace(str,">",">")
str=Replace(str,"<","<")
str=Replace(str,Chr(32)," ")
str=Replace(str,Chr(9)," ")
str=Replace(str,Chr(34),""")
str=Replace(str,Chr(39),"'")
str=Replace(str,Chr(13),"")
str=Replace(str,Chr(10) & Chr(10), "</p><p>")
str=Replace(str,Chr(10),"<br> ")
HtmlEncode=str
end if
End Function
'****************************************************************************
'' @功能说明: 计算源字符串Str的长度(一个中文字符为2个字节长)
'' @参数说明: - str [string]: 源字符串
'' @返回值: - [Int] 源字符串的长度
'****************************************************************************
Public Function strLen(Str)
If Trim(Str)="" Or IsNull(str) Then
strlen=0
else
Dim P_len,x
P_len=0
StrLen=0
P_len=Len(Trim(Str))
For x=1 To P_len
If Asc(Mid(Str,x,1))<0 Then
StrLen=Int(StrLen) + 2
Else
StrLen=Int(StrLen) + 1
End If
Next
end if
End Function
'****************************************************************************
'' @功能说明: 截取源字符串Str的前LenNum个字符(一个中文字符为2个字节长)
'' @参数说明: - str [string]: 源字符串
'' @参数说明: - LenNum [int]: 截取的长度
'' @返回值: - [string]: 转换后的字符串
'****************************************************************************
Public Function CutStr(Str,LenNum)
Dim P_num
Dim I,X
If StrLen(Str)<=LenNum Then
Cutstr=Str
Else
P_num=0
X=0
Do While Not P_num > LenNum-2
X=X+1
If Asc(Mid(Str,X,1))<0 Then
P_num=Int(P_num) + 2
Else
P_num=Int(P_num) + 1
End If
Cutstr=Left(Trim(Str),X)&"..."
Loop
End If
End Function
end class
%>
4 楼
hbgth [专家分:30] 发布于 2004-07-08 13:41:00
你现在才发现啊 不会吧 我一开始学ASP就用VBscript做了class了呢
5 楼
greatsun [专家分:280] 发布于 2004-07-09 08:32:00
谢谢,圪圪!你要结婚了吗?恭喜你了![em12][em2][em4][em12]
6 楼
skyhaha [专家分:260] 发布于 2004-07-10 05:59:00
晕,ASP.NET的类还是要简单一点
不管怎么说来顶一下。
7 楼
skyhaha [专家分:260] 发布于 2004-07-13 07:53:00
好贴没人顶,我来帮你,哈哈
8 楼
skyhaha [专家分:260] 发布于 2004-07-13 12:25:00
继续
9 楼
略知皮毛 [专家分:11790] 发布于 2004-07-13 23:51:00
不错。
10 楼
roe [专家分:0] 发布于 2004-07-21 09:57:00
我什么都不懂,以后请大家多多指教!
我来回复