zoukankan      html  css  js  c++  java
  • 在ASP中使用类(Class)

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