首先asp的(vbscript)類是由事件和方法(它們就是構成類的成員了)構成的,如果大家還沒有接觸過,可以先看看下面的說明(哈哈,我是現學現賣,說得不好請見諒)
在class 塊中,成員通過相應的聲明語句被聲明為private(私有成員,只能在類內部調用) 或public(公有成員,可以在類內外部調用) 。被聲明為private 的將只在class 塊內是可見的。被聲明為public 不僅在class 塊的內部是可見的,對class 塊之外的代碼也是可見的。沒有使用private 或public 明確聲明的被默認為public。在類的塊內部被聲明為public 的過程(sub 或function)將成為類的方法。 public 變量將成為類的屬性,同使用property get、property let 和property set 顯式聲明的屬性一樣。類的缺省屬性和方法是在它們的聲明部分用default 關鍵字指定的。
請大家內心看完藍色的部分,下面我們來看一個例子
<script language=vbscript runat=server>
class myclass
'//----聲明(聲明就是定義)myclass類的類內部(私有的[private])變量
private strauthor
private strversion
private strexample
'//---------------------------定義類的事件---------------- ---------------//
'//----class_initialize()是類的初始化事件,只要一開始使用該類,首先會觸發該部分的執行,下面我們會在該成員中初始化該類的作者和版本以及在屏幕上顯示一下該類已經開始了
private sub class_initialize()
strauthor = 思源
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:[email protected]'>maxid_zen</a> @ <a href='http://www.design60s.com'>www.design60s.com</ 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
</script>
<%
'//-------這裡是使用該類的例子
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
%>