69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

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

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

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

屬性,方法和事件

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

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

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

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

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

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

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

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

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

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

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

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

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

這里先聲明了一個(gè)名為 blueidea的類(lèi),接著是一些私有變量,用于存儲(chǔ)blueidea類(lèi)的屬性,這些變量在代碼的外部不能訪(fǎng)問(wèn),這就是數(shù)據(jù)保護(hù),要定義這些變量,使用了property語(yǔ)句獲得值間接的付給私有變量

''''-----------------------------------------------------------------
property get getname
getname=bname
end property

property let getname(nameid)
bname=nameid
if nameid="" then
bname="沒(méi)注冊(cè)用戶(hù)"
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ù)功能很明確,不再繁瑣說(shuō)。

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

private function getlevel()
bpoint=killint(bpoint,0,0)
if bpoint<500 then
blevel="初級(jí)會(huì)員"
elseif bpoint>=500 and bpoint<=100 then
blevel="高級(jí)會(huì)員"
else
blevel="終極會(huì)員"
end if
getlevel=blevel
end function

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

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

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

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

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

''''名稱(chēng):ado_5do8
''''作用:讀取數(shù)據(jù)庫(kù)的各項(xiàng)操作
''''來(lái)源-耕耘村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)站積分超過(guò)3000,耕耘村所有注冊(cè)用戶(hù)
''''類(lèi)的接口:ado_5do8.connectstring=數(shù)據(jù)庫(kù)絕對(duì)路徑
 ''''ado_5do8.rs_top 調(diào)用數(shù)目,表的名稱(chēng) 
class ado_5do8
private conn,sqlstr,rs,iid,itable,isession
  ''''sqlstr:數(shù)據(jù)庫(kù)地址,為絕對(duì)路徑,私有
  ''''conn:打開(kāi)數(shù)據(jù)庫(kù)的連接,私有

''''------------------------------------------------------------------
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ù)庫(kù)"
  response.write(mess)
  response.end
  else
  mess="連接數(shù)據(jù)庫(kù)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ù)庫(kù)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ù):每頁(yè)數(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)文章