[ASP]使用類,實(shí)現(xiàn)模塊化
所有寫程序的人都知道,當(dāng)你逐漸對您要實(shí)現(xiàn)的功能展開的時(shí)候,很大的時(shí)候,第一天寫的東西第二天就忘了寫到那里了,很多的時(shí)候,不得不寫上詳細(xì)的程序開發(fā)筆記,這在asp的系統(tǒng)開發(fā)中感覺尤其文件、函數(shù)復(fù)雜的時(shí)候,當(dāng)我們打算對網(wǎng)站的一部分功能進(jìn)行修改的時(shí)候,感覺無從下手或者感覺要修改的地方。這時(shí)候,如果您學(xué)過任何一門面向?qū)ο蟮木幊痰恼Z言的話,自然想到怎么能把代碼功能實(shí)現(xiàn)模塊話,asp本質(zhì)上不是面向?qū)ο蟮木幊蹋玽bscrpit6.0提供了類,我們可以通過類實(shí)現(xiàn)代碼的封裝,實(shí)現(xiàn)模塊話。

首先,我要在這里寫上一些很官方的概念,意在說明面向?qū)ο笫呛芫唧w化的,很實(shí)體的模式,不能讓有些人看見“對象”就被嚇跑了。

對象,就是能看到,感到,聽到,觸摸到,嘗到或聞到的東西,在這里我們這樣“定義”:對象是一個(gè)自包含的實(shí)體,用一組可識別的特性和行為來標(biāo)識。
在面向?qū)ο蟮木幊?oop)的編程方式,用使用下面的兩個(gè)術(shù)語。
類:這是對象的模板,定義了對象的特性。
實(shí)例:這是一個(gè)真實(shí)的對象,可以與之交互的東西。

屬性,方法和事件

在oop中,下面的術(shù)語描述對象的特性:

屬性:這是一個(gè)名次,描述了某個(gè)對象的屬性。

方法:這是一個(gè)動(dòng)詞,描述了對象可以完成的工作,或者希望它完成的工作。

事件:描述了對象為相應(yīng)某個(gè)動(dòng)作而執(zhí)行的操作。
在編程時(shí),對象的面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計(jì)的一部分,它們具有非常大的優(yōu)勢,許多人認(rèn)為這是一個(gè)復(fù)雜的主題,但實(shí)際上,它非常簡單,可以用四個(gè)簡單的術(shù)語來解釋:抽象、封裝、多態(tài)和繼承。

抽象:這是一個(gè)隱藏復(fù)雜性,類的內(nèi)部工作情況,所以用戶不必知道它的運(yùn)作方式,就像。如果想要看電視,就不必知道電視機(jī)時(shí)如何工作的,只需打開電視機(jī),搜索頻道即可,on/off開關(guān)抽象了實(shí)際的操作,在string例子里,有一個(gè)trim方法,它可以刪除字符串尾部的空格,同樣不需要知道他是如何完成這個(gè)任務(wù)的,只要知道它有這個(gè)功能即可。

封裝:每個(gè)對象都包含進(jìn)行操作所需要的所有信息,這個(gè)對象稱為封裝,因此對象不比依賴其他對象來完成自己的操作,在術(shù)語toupper()方法中,string不必到其他地方獲取信息來把所有的字符轉(zhuǎn)換為大寫。

多態(tài):這個(gè)術(shù)語用于表示不同的對象可以執(zhí)行相同的動(dòng)作,但要通過他們自己的實(shí)現(xiàn)代碼來執(zhí)行,名稱一樣,但底層實(shí)現(xiàn)的代碼是不一樣的。

繼承:它定義了類如何相互關(guān)聯(lián),共享特性的,繼承的工作方式是,定義類和子類,其中子類繼承了父類的所有特性,繼承的重要性是,它迫使類型相似的類具有一致性,并允許共享代碼,如果決定創(chuàng)建一個(gè)新類,就不必定義父類的所有特性。

在asp中使用類,實(shí)現(xiàn)模塊化

下面我通過舉上幾個(gè)簡單的例子說明一下,注意,這里強(qiáng)調(diào)的是一種思想,如果在您開發(fā)asp網(wǎng)站的時(shí)候能用一個(gè)類(基類)展開的話,這是很有必要的(也是很有難度的)。

我們先選擇一個(gè)簡單的例子:

我們要顯示經(jīng)典論壇用戶的信息,當(dāng)輸入用戶的id以后能,顯示出該用戶的一些信息,這是一個(gè)過程,可以這樣考慮,我們把用戶當(dāng)作一個(gè)對象,他有的屬性是id,性別,積分,權(quán)限,實(shí)現(xiàn)的方法有顯示這些信息,ok,這樣寫:

class blueidea
private bname,bpoint,bsex,blevel
''''...................
end class

這里先聲明了一個(gè)名為 blueidea的類,接著是一些私有變量,用于存儲blueidea類的屬性,這些變量在代碼的外部不能訪問,這就是數(shù)據(jù)保護(hù),要定義這些變量,使用了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
''''------------------------------------------------------------------

這里有個(gè)killint函數(shù),是判斷數(shù)據(jù)合法性的,它的原形是:

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

該函數(shù)功能很明確,不再繁瑣說。

由于我們要通過積分判斷用戶級別,這里定義了一個(gè)私有函數(shù):

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

我們要得是回送用戶的信息,必須定義一個(gè)public公用函數(shù),顯示信息:

public function showuser()
response.write("以下顯示"&bname&"的資料: ")
response.write("性別:"&bsex&" ")
response.write("積分:"&bpoint&" ")
getlevel
response.write("級別:"&blevel&" ")
end function
end class

使用這個(gè)類的時(shí)候這樣使用:(我在這里寫了一個(gè)表單處理的)

set blueideauser=new blueidea
blueideauser.getname=trim(request("id"))
blueideauser.getsex=request("sex")
blueideauser.getpoint=request("point")
blueideauser.showuser

控制讀取數(shù)據(jù)庫信息的類:
參考源碼:

''''名稱:ado_5do8
''''作用:讀取數(shù)據(jù)庫的各項(xiàng)操作
''''來源-耕耘村http://www.5do8.com http://www.blueidea.com-5do8
''''創(chuàng)作:5do8
''''聯(lián)系:5do8@5do8.com
''''更新:2005年11月13日
''''授權(quán):藍(lán)色理想網(wǎng)站積分超過3000,耕耘村所有注冊用戶
''''類的接口:ado_5do8.connectstring=數(shù)據(jù)庫絕對路徑
 ''''ado_5do8.rs_top 調(diào)用數(shù)目,表的名稱 
class ado_5do8
private conn,sqlstr,rs,iid,itable,isession
  ''''sqlstr:數(shù)據(jù)庫地址,為絕對路徑,私有
  ''''conn:打開數(shù)據(jù)庫的連接,私有

''''------------------------------------------------------------------
rem 消除一些不想要的數(shù)字  
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="發(fā)生錯(cuò)誤,不能連接數(shù)據(jù)庫"
  response.write(mess)
  response.end
  else
  mess="連接數(shù)據(jù)庫conn成功...........
"
  response.write(mess)
  end if
  end sub
''''----------------------------------------------------------------
private sub closeconn()
  conn.close 
  set conn=nothing
  response.write("關(guān)閉conn連接... ")
 end sub 
''''-----------------------------------------------------------------
private sub closers()
 rs.close
 set rs=nothing
 response.write("關(guān)閉數(shù)據(jù)庫rs.......
")

 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("總數(shù)獲得了:")
 response.write(" "&irows+1&"條信息 ")
 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 (""&data(1, irowloop) & " 較慢,不推薦點(diǎn)擊-->更新")
 next 
 response.write "
列表(回到典型模式):"
 if start > 0 then
  response.write "previous"
 end if 
 if istop < irows then
 response.write " next"
 end if

 response.write""

 else
 rowid=litter_in(rowid-1,irows)
 response.write("
返回列表 "&server.htmlencode(data(1,rowid))&"

"&server.htmlencode(data(2,rowid))&"

+-----"&server.htmlencode(data(3,rowid))&"")
 response.write("
")
 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()
%> 
 
  顯示信息總數(shù):每頁數(shù)目:

   操作:


 <%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("
返回默認(rèn)模式 ")
    response.write"下面顯示具體信息: "
   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"
相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频