首先ASP的类是由事件和方法(它们就是构成类的成员了)构成的,ASP类比较简单,它不具有真正意义类的继承、重载等特性,尽管如此,它仍然是非常有用的,它降低了我们编程的复杂度,使我们的程序模块更加合理。 它支持以下内容:
公有成员变量、私有成员变量、属性
公有成员函数、私有成员函数
构造函数、析构函数
在 Class 块中,成员通过相应的声明语句被声明为 Private(私有成员,只能在类内部调用) 或 Public(公有成员,可以在类内外部调用) 。被声明为 Private 的将只在 Class 块内是可见的。被声明为 Public 不仅在 Class 块的内部是可见的,对 Class 块之外的代码也是可见的。没有使用 Private 或 Public 明确声明的被默认为 Public。在类的块内部被声明为 Public 的过程(Sub 或 Function)将成为类的方法。Public/Private 变量将成为类的属性,同使用 Property Get、Property Let 和 Property Set 显式声明的属性一样。类的缺省属性和方法是在它们的声明部分用 Default 关键字指定的。
注:关于几个声明语句
Public 声明公用变量
Private 声明私有变量
Property Let 是设置变量用的
Property Set 是设置对象用的
Property Get 是取回属性用的
下面是很好的一个关于类的例子:
'//--------------------------------开始一个类---------------------------------// Class myClass '//----声明(声明就是定义)myClass类的类内部(私有的[Private])变量 Private strAuthor Private strVersion Private strExample '//---------------------------定义类的事件-------------------------------// '//----Class_Initialize()是类的初始化事件,只要一开始使用该类,首先会触发该部分的执行,下面我们会在该成员中初始化该类的作者和版本以及在屏幕上显示一下该类已经开始了 '//----该函数为构造函数,在使用 set new 创建对象时,自动执行 Private Sub Class_Initialize() strAuthor = "coldstone" strVersion = "1.0" Response.Write "myClass开始了" End Sub '//----Class_Terminate()是类的结束事件,只要一退出该类,就会触发该事件,下面我们会该事件中设定退出该类时会在屏幕上显示该类已结束了。 '//----该函数为析构函数,在使用 set nothing 释放对象时,自动执行 Private Sub Class_Terminate() Response.Write "myClass结束了" End Sub '//---------------------------用户自己定义的方法-------------------------------// '//----该方法返回一个版权信息 Public Sub Information() Response.Write "Copyright By coldstone @ 闪吧." End Sub '//---------------------------定义类的输出属性-------------------------------// '//----定类的属性,该属性是让用户初始化strExapmle变量 '//----property let声明了当前属性是:写属性 Public Property Let setExapmle(ByVal strVar) strExapmle = strVar End Property '//---------------------------定义类的输出属性-------------------------------// '//----定义类的属性,该属性是返回一个版本号 '//----Property Get声明了当前属性是:读属性 Public Property Get Version Version = strVersion End Property '//----定义类的属性,该属性是返回该类的作者号 Public Property Get Author Author = strAuthor End Property '//----定义类的属性,该属性是返回一个版本号 Public Property Get Exapmle Exapmle = strExapmle End Property End Class '//-------这里是使用该类的例子 Dim oneNewClass Set oneNewClass = New myClass Response.Write "作者: "& oneNewClass.Author &"<br>" Response.Write "版本: "& oneNewClass.Version &"<br>" oneNewClass.setExapmle = "这是一个简单类的例子" Response.Write "用户自定义:" & oneNewClass.Exapmle &"<br>" oneNewClass.Information Set oneNewClass = Nothing
ASP类的功能实在有限,所以有时我们要实现复杂的功能,只好用组合来实现。
class CFish sub Swim() end sub end class class CBird sub Fly() end sub end class class CAnimal dim fish dim bird private sub class_initialize() set fish = new CFish set bird = new CBird end sub private sub class_terminate() set fish = nothing set bird = nothing end sub end class dim animal set animal = new CAnimal call animal.fish.Swim() '鱼游 call animal.bird.Fly() '鸟飞 set animal = nothing
相信大家现在对ASP类有一定的了解了,但上面都没提到到Property Set声明语句的用法,下面我再举一个例子:
Class Class1 Private mrstRecordSet Public Property Set RecordSet(Value) If UCase(TypeName(Value)) = "RECORDSET" Then Set mrstRecordSet = Value End If End Property Public Function IsClose IsClose = False If mrstRecordSet.State = 0 Then IsClose = True End If End Function End Class '创建对象 Set cls1 = New Class1 '创建RS Set rs = Server.CreateObject("ADODB.Recordset") 'cls1的RecordSet属性引用创建的RS Set cls1.RecordSet = rs '判断RS是否关闭 Response.Write cls1.IsClose