-
asp 类写法
网络 2014/6/23 9:39:45首先ASP的类是由事件和方法(它们就是构成类的成员了)构成的,如果大家还没有接触过,可以先看看下面的说明:
在 Class 块中,成员通过相应的声明语句被声明为 Private(私有成员,只能在类内部调用) 或 Public(公有成员,可以在类内外部调用) 。被声明为 Private 的将只在 Class 块内是可见的。被声明为 Public 不仅在 Class 块的内部是可见的,对 Class 块之外的代码也是可见的。没有使用 Private 或 Public 明确声明的被默认为 Public。在类的块内部被声明为 Public 的过程(Sub 或 Function)将成为类的方法。Public 变量将成为类的属性,同使用 Property Get、Property Let 和 Property Set 显式声明的属性一样。类的缺省属性和方法是在它们的声明部分用 Default 关键字指定的。
请大家耐心看完上面的部分,下面我们来看一个例子:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253<%
'//--------------------------------开始一个类---------------------------------//
Class
myClass
'//----声明(声明就是定义)myClass类的类内部(私有的[Private])变量
Private
strAuthor
Private
strVersion
Private
strExample
'//---------------------------定义类的事件-------------------------------//
'//----Class_Initialize()是类的初始化事件,只要一开始使用该类,首先会触发该部分的执行,下面我们会在该成员中初始化该类的作者和版本以及在屏幕上显示一下该类已经开始了
Private
Sub
Class_Initialize()
strAuthor =
"coldstone"
strVersion =
"1.0"
Response.Write
"<br>myClass开始了<br>"
End
Sub
'//----Class_Terminate()是类的结束事件,只要一退出该类,就会触发该事件,下面我们会该事件中设定退出该类时会在屏幕上显示该类已结束了。
Private
Sub
Class_Terminate()
Response.Write
"<br>myClass结束了<br>"
End
Sub
'//---------------------------用户自己定义的方法-------------------------------//
'//----该方法返回一个版权信息
Public
Sub
Information()
Response.Write
"<br>Coding By <a href='mailto:coldstone@falsh8.cn'>coldstone</a> @ <a href='http://www.flash8.net'>闪吧</a>.<br>"
End
Sub
'//---------------------------定义类的输出属性-------------------------------//
'//----定类的属性,该属性是让用户初始化strExapmle变量
Public
Property
Let
setExapmle(
ByVal
strVar)
strExapmle = strVar
End
Property
'//---------------------------定义类的输出属性-------------------------------//
'//----定义类的属性,该属性是返回一个版本号
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
%>
阅读(825) 分享(0)