寫在前面的話:
所有寫程式的人都知道,當你逐漸對您要實現的功能展開的時候,很大的時候,第一天寫的東西第二天就忘了寫到那裡了,很多的時候,不得不寫上詳細的程式開發筆記,這在ASP的系統開發中感覺尤其文件、函數複雜的時候,當我們打算對網站的一部分功能進行修改的時候,感覺無從下手或感覺要修改的地方。這時候,如果您學過任何一門面向對象的編程的語言的話,自然想到怎麼能把代碼功能實現模組話,asp本質上不是面向對象的編程,但VBSCRPIT6.0提供了類,我們可以通過類實現程式碼的封裝,實作模組話。
首先,我要在這裡寫上一些很官方的概念,意在說明物件導向是很具體化的,很實體的模式,不能讓有些人看見對象就被嚇跑了。
對象,就是能看到,感到,聽到,觸摸到,嚐到或聞到的東西,在這裡我們這樣定義:對像是一個自包含的實體,用一組可識別的特性和行為來標識。
在物件導向的程式設計(oop)的程式設計方式,用使用下面的兩個術語。
類別:這是物件的模板,定義了物件的特性。
實例:這是一個真實的對象,可以與之互動的東西。
屬性,方法和事件
在OOP中,以下的術語描述物件的特性:
屬性:這是一個名次,描述了某個物件的屬性。
方法:這是一個動詞,描述了物件可以完成的工作,或希望它完成的工作。
事件:描述了物件為對應某個動作而執行的操作。
在程式設計時,物件的物件導向程式設計和物件導向設計的一部分,它們具有非常大的優勢,許多人認為這是一個複雜的主題,但實際上,它非常簡單,可以用四個簡單的術語來解釋:抽象、封裝、多型和繼承。
抽象:這是一個隱藏複雜性,類別的內部工作情況,所以使用者不必知道它的運作方式,就像。如果想要看電視,就不必知道電視機時如何運作的,只需打開電視機,搜尋頻道即可,on/off開關抽象了實際的操作,在string例子裡,有一個trim方法,它可以刪除字串尾部的空格,同樣不需要知道他是如何完成這個任務的,只要知道它有這個功能即可。
封裝:每個物件都包含進行操作所需的所有信息,這個物件稱為封裝,因此物件不比依賴其他物件來完成自己的操作,在術語TOupper()方法中,string不必到其他地方獲取資訊來把所有的字符轉換為大寫。
多態:這個術語用來表示不同的物件可以執行相同的動作,但要透過他們自己的實作程式碼來執行,名稱一樣,但底層實作的程式碼是不一樣的。
繼承:它定義了類別如何相互關聯,共享特性的,繼承的工作方式是,定義類別和子類,其中子類繼承了父類的所有特性,繼承的重要性是,它迫使類型相似的類別具有一致性,並允許共享程式碼,如果決定創建一個新類,就不必定義父類的所有特性。
在ASP使用類,實現模組化
下面我舉上幾個簡單的例子來說明一下,注意,這裡強調的是一種思想,如果在您開發ASP網站的時候能用一個類別(基類)展開的話,這是很有必要的(也是很有難度的)。
我們先選擇一個簡單的例子:
我們要顯示經典論壇用戶的信息,當輸入用戶的ID以後能,顯示出該用戶的一些信息,這是一個過程,可以這樣考慮,我們把用戶當作一個對象,他有的屬性是ID,性別,積分,權限,實現的方法有顯示這些信息,ok,這樣寫:
Class blueidea
Private bname,bpoint,bsex,blevel
'...................
end class
這裡先聲明了一個名為blueidea的類,接著是一些私有變量,用於存儲blueidea類的屬性,這些變量在代碼的外部不能訪問,這就是數據保護,要定義這些變量,使用了property語句獲得值間接的付給私有變數
'------------------------------------------------- ----------------
Property Get getname
getname=bname
End Property
Property Let getname(nameid)
bname=nameid
If nameid= Then
bname=沒註冊用戶
End If
End Property
'------------------------------------------------- -----------------
Property Get getsex
getsex=bsex
End Property
Property Let getsex(sex)
bsex=killint(sex,0,0)
If bsex=0 Then
bsex=男
Else
bsex=女
End if
End Property
'------------------------------------------------- -----------------
Property Get getpoint
getpoint=bpoint
End Property
Property Let getpoint(point)
bpoint=killint(point,0,0)
End Property
'------------------------------------------------- -----------------
這裡有個killint函數,是判斷資料合法性的,它的原形是:
Private Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function
該函數功能很明確,不再繁瑣說。
由於我們要透過積分判斷使用者級別,這裡定義了一個私有函數:
Private Function getlevel()
bpoint=killint(bpoint,0,0)
If bpoint<500 Then
blevel=初級會員
ElseIf bpoint>=500 And bpoint<=100 Then
blevel=高級會員
Else
blevel=終極會員
End If
Getlevel=blevel
End Function
我們要得是回送使用者的訊息,必須定義一個public公用函數,顯示訊息:
Public Function showuser()
response.write(<h5>以下顯示<font color=red>&bname&</font>的資料:</h5>)
response.write(<h5>性別:<font color=red>&bsex&</font></h5>)
response.write(<h5>積分:<font color=red>&bpoint&</font></h5>)
getlevel
response.write(<h5>等級:<font color=red>&blevel&</font></h5>)
End Function
End class
使用這個類別的時候這樣使用:(我在這裡寫了一個表單處理的)
Set blueideauser=new blueidea
blueideauser.getname=Trim(request(id))
blueideauser.getsex=request(sex)
blueideauser.getpoint=request(point)
blueideauser.showuser
是不是想看效果,那就看看這裡: http://www.5do8.com/net/aspclass/class.asp
控制讀取資料庫資訊的類別:
參考來源碼
'名稱:ado_5do8
'作用:讀取資料庫的各項操作
'來源-耕耘村http://www.5do8.com http://www.Blueidea.com-5do8
'創作:5do8
'更新:2005年11月13日
'授權:藍色理想網站積分超過3000,耕耘村所有註冊用戶
'類別的介面:ado_5do8.ConnectString=資料庫絕對路徑
'ado_5do8.rs_top 呼叫數目,表格的名稱
Class ado_5do8
Private conn,sqlstr,rs,iid,itable,isession
'sqlstr:資料庫位址,為絕對路徑,私有
'conn:開啟資料庫的連接,私有
'------------------------------------------------- -----------------
rem 消除一些不想要的數字
Private Function litter_in(r1,r2)
If IsNumeric(r1) and IsNumeric(r2) Then
Dim dimrr
If r1>r2 Then
dimrr=r2
Else
dimrr=r1
End If
Else
dimrr=0
End if
litter_in=dimrr
End Function
'------------------------------------------------- ----------------
Private Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function
'------------------------------------------------- ----------
private Sub startconn()
On Error Resume Next
Set conn=server.CreateObject(adodb.connection)
strconn=Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & Server.MapPath(sqlstr)
conn.open strconn
If Err Then
err.Clear
Set Conn = Nothing
mess=發生錯誤,無法連接資料庫
response.write(mess)
response.End
Else
mess=連接資料庫conn成功...........<br/>
response.write(mess)
End If
End Sub
'------------------------------------------------- ---------------
private Sub closeconn()
conn.close
Set conn=Nothing
response.write(<strong style='color:red'>關閉conn連線</strong>...<hr/>)
End sub
'------------------------------------------------- ----------------
Private Sub closers()
rs.close
Set rs=Nothing
response.write(<strong style='color:#085420'>關閉資料庫RS</strong>.......<br/>)
End Sub
'------------------------------------------------- ----------------
Property Get havese
havese=isession
End Property
Property Let havese(yoursession)
isession=yoursession
If yoursession= Then
isession=nodef
End If
End Property
'------------------------------------------------- ----------------
Public Function makesession(arraydata)
If IsArray(arraydata) then
makear=arraydata
Else
makear=Array(0,0,0,0)
End If
If isession= Then
isession=nodef
End if
session(isession)=makear
End Function
'------------------------------------------------- ----------------
private Function getsession()
thisget=session(isession)
If Not IsArray(thisget) Then
thisget=Array(0,0,0,0)
End If
Getsession=thisget
End function
'------------------------------------------------- ----------------
Property Get ConnectString
ConnectString = sqlstr
End Property
Property Let ConnectString(str)
sqlstr = str
End Property
'------------------------------------------------- ----------------
Property Get getid
getid = iid
End Property
Property Let getid(id)
iid = id
End Property
'------------------------------------------------- ----------------
Property Get gettable
gettable = itable
End Property
Property Let gettable(table)
itable = table
End Property
'------------------------------------------------- ----------------
'------------------------------------------------- -----------------
public Function readarraysession(iStart,ipageno,irowid)
rowid=killint(irowid,0,0)
start=killint(istart,0,0)
pageno=killint(ipageno,5,5)
data=getsession
iRows = UBound(data, 2)
iCols = UBound(data, 1)
response.write(<h5>總數獲得了:)
response.write(<b> &iRows+1&</b>條訊息</h5><hr/><ul style='width:100%;'>)
If rowid = 0 then
If iRows > (ipageno + iStart) Then
iStop = ipageno + iStart - 1
Else
iStop = iRows
End If
For iRowLoop = Start to iStop
Response.Write (<li style='padding:4px 0;'><a href=?k=read&rowid=&irowloop+1&>&data(1, iRowLoop) & </a><span style='padding:4px 0 4px 10px;background-color:#ccc; '>較慢,不建議點擊--><a href=?k=list&id=&data(0,irowloop)&>更新</a></span></li>)
Next
Response.Write </ul><div style='top:20px;background-color:#ccc;color:#020;font-weight:bold;bordr-top:2px solid #008;padding:10px 0;color: #b00'>列表(<a href=default.asp>回到典型模式</a>):
if Start > 0 then
Response.Write <A HREF=?k=read&Start= & iStart-ipageno &&pageno= & ipageno & >Previous</A>
end if
if iStop < iRows then
Response.Write <A HREF=?k=read&Start= & iStart+ipageno &&pageno= & ipageno & >Next</A>
end If
response.write</div>
Else
rowid=litter_in(rowid-1,iRows)
response.write(<div style='width:85%'><h4 style='text-align:center'><a href=?k=read&pageno=&pageno&&start=&start&>回傳清單</a></h4></h2><hr/><h5>&server.htmlencode(data(1,rowid))&</h5><p >&server.htmlencode(data(2,rowid))&<h5>+-----&server.htmlencode(data(3,rowid))&)
response.write(<div >)
End if
End Function
'------------------------------------------------- ----------------
Public Function list_ids()
sql3=select * from &itable& where id=&iid&
startconn()
Set rs=conn.execute(sql3)
If rs.eof And rs.bof Then
data=Array(0,0,0,0)
Else
data=Rs.GetRows()
End If
closers
closeconn
response.write(UBound(data)&:)
response.write(server.htmlencode(data(2,0)))
End function
'------------------------------------------------- ----------------
Public Function rs_top(num,table,whe)
startconn()
sql=select top &num& * from &table&
sql2=select count(*) as szd_count from &table& &whe&
Set rs=conn.execute(sql2)
szd_count=rs(szd_count)
closers
Set rs = Conn.Execute(sql)
dim data
If Rs.Eof Then
data=no data
Else
data=Rs.GetRows()
End if
closers
closeconn()
Call makesession (data)
End Function
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
End Class
使用的時候:
Dim action
action=request(k)
If action=view Then
Call viewnew
ElseIf action=list Then
Call list()
ElseIf action=read Then
Call read()
Else
Call ff()
End if
Sub ff()
%>
<form style=border-top:2px solid #008;border-bottom:2px solid #008;margin:auto;background-color:#eee;padding:20px 5px;color:#008;font-weight:bold;>
<label>顯示資訊總數:<input name=n type=text maxlength=4 size=10 />每頁數目:<input name=pagesize type=text maxlength=4 size=10 value=5/><input name =arrstart type=hidden value=0></label>
<h5 style=border-top:1px solid #000;padding:5px 0> 操作:<input name=k type=submit value=view /></h5>
</form> <%End sub%>
<%Sub viewnew()
f_num=killint(request(n),1,1)
pagesize=killint(request(pageno),5,5)
arrstart=killint(request(start),0,0)
rowid=killint(request(rowid),0,0)
Set cs=new ado_5do8
cs.ConnectString=data/a.mdb
cs.havese=shi
cs.rs_top f_num,site_szd,
cs.readarraysession arrstart,pagesize,rowid
End sub
Sub list()
response.write(<h5><a href=default.asp>傳回預設模式</a></h5>)
response.write下面顯示具體資訊:<hr/>
id=request(id)
id=killint(id,1,1)
Set listid=new ado_5do8
listid.ConnectString=data/a.mdb
listid.getid=id
listid.gettable=site_szd
listid.list_ids()
End Sub
Sub read()
response.write<div style='background-color:#ccc;padding:20px 0;color:080;font-weight:bold;border-bottom:2px solid #008'>頁面分析完畢,請更新請選擇<a href=default.asp>回到典型模式</a>參數:Start,開始元素;pageno,每頁條數</div>
pagesize=killint(request(pageno),5,5)
arrstart=killint(request(start),0,0)
rowid=killint(request(rowid),0,0)
Set cs=new ado_5do8
cs.havese=shi
cs.readarraysession arrstart,pagesize,rowid
End sub
Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function
%>