濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > [ASP]使用類,實(shí)現(xiàn)模塊化

[ASP]使用類,實(shí)現(xiàn)模塊化

熱門標(biāo)簽:外呼系統(tǒng)API接口 修改地圖標(biāo)注 武夷山旅游地圖標(biāo)注 金昌電話機(jī)器人價(jià)格 個(gè)人可以辦理400電話么 怎么在地圖標(biāo)注自己 縣域地圖標(biāo)注打印店 鳳臺(tái)百度地圖標(biāo)注店 萊西電子地圖標(biāo)注
所有寫程序的人都知道,當(dāng)你逐漸對(duì)您要實(shí)現(xiàn)的功能展開的時(shí)候,很大的時(shí)候,第一天寫的東西第二天就忘了寫到那里了,很多的時(shí)候,不得不寫上詳細(xì)的程序開發(fā)筆記,這在ASP的系統(tǒng)開發(fā)中感覺(jué)尤其文件、函數(shù)復(fù)雜的時(shí)候,當(dāng)我們打算對(duì)網(wǎng)站的一部分功能進(jìn)行修改的時(shí)候,感覺(jué)無(wú)從下手或者感覺(jué)要修改的地方。這時(shí)候,如果您學(xué)過(guò)任何一門面向?qū)ο蟮木幊痰恼Z(yǔ)言的話,自然想到怎么能把代碼功能實(shí)現(xiàn)模塊話,asp本質(zhì)上不是面向?qū)ο蟮木幊?,但VBSCRPIT6.0提供了類,我們可以通過(guò)類實(shí)現(xiàn)代碼的封裝,實(shí)現(xiàn)模塊話。

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

對(duì)象,就是能看到,感到,聽(tīng)到,觸摸到,嘗到或聞到的東西,在這里我們這樣“定義”:對(duì)象是一個(gè)自包含的實(shí)體,用一組可識(shí)別的特性和行為來(lái)標(biāo)識(shí)。
在面向?qū)ο蟮木幊?oop)的編程方式,用使用下面的兩個(gè)術(shù)語(yǔ)。
類:這是對(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)解釋:抽象、封裝、多態(tài)和繼承。

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

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

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

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

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

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

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

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

Class blueidea
Private bname,bpoint,bsex,blevel
''''...................
end class

這里先聲明了一個(gè)名為 blueidea的類,接著是一些私有變量,用于存儲(chǔ)blueidea類的屬性,這些變量在代碼的外部不能訪問(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è)用戶"
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ò)積分判斷用戶級(jí)別,這里定義了一個(gè)私有函數(shù):

Private Function getlevel()
bpoint=killint(bpoint,0,0)
If bpoint500 Then
blevel="初級(jí)會(huì)員"
ElseIf bpoint>=500 And bpoint=100 Then
blevel="高級(jí)會(huì)員"
Else
blevel="終極會(huì)員"
End If
Getlevel=blevel
End Function

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

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>級(jí)別:font color=red>"blevel"/font>/h5>")
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ù)庫(kù)信息的類:
參考源碼:

''''名稱: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è)用戶
''''類的接口:ado_5do8.ConnectString=數(shù)據(jù)庫(kù)絕對(duì)路徑
 ''''ado_5do8.rs_top 調(diào)用數(shù)目,表的名稱 
Class ado_5do8
Private conn,sqlstr,rs,iid,itable,isession
  ''''sqlstr:數(shù)據(jù)庫(kù)地址,為絕對(duì)路徑,私有
  ''''conn:打開數(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("strong style=''''color:red''''>關(guān)閉conn連接/strong>...hr/>")
 End sub 
''''-----------------------------------------------------------------
Private Sub closers()
 rs.close
 Set rs=Nothing
 response.write("strong style=''''color:#085420''''>關(guān)閉數(shù)據(jù)庫(kù)RS/strong>.......
")

 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>總數(shù)獲得了:")
 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=readrowid="irowloop+1">"data(1, iRowLoop)  " /a>span style=''''padding:4px 0 4px 10px;background-color:#ccc; ''''>較慢,不推薦點(diǎn)擊-->a href=?k=listid="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=readStart="  iStart-ipageno "pageno="  ipageno  """>Previous/A>"
 end if 
 if iStop  iRows then
 Response.Write " A HREF=""?k=readStart="  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=readpageno="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>顯示信息總數(shù):input name="n" type="text" maxlength="4" size="10" />每頁(yè)數(shù)目: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>返回默認(rèn)模式/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''''>頁(yè)面分析完畢,要更新請(qǐng)選擇a href=default.asp>回到典型模式/a>參數(shù):Start,開始元素;pageno,每頁(yè)條數(shù)/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 
%>

說(shuō)明:

此源碼5do8單獨(dú)寫出,本源碼我享有解釋權(quán),但不保證源碼的安全,任何損失使用者自己承擔(dān),本源碼僅僅限于在耕耘村(http: //www.5do8.com),藍(lán)色理想(http://www.blueidea.com)和締客論壇(http://www.dw8.cn)站內(nèi)交流。當(dāng)然,不許抄襲。

標(biāo)簽:通遼 涼山 上海 南京 邢臺(tái) 赤峰 清遠(yuǎn) 楚雄

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《[ASP]使用類,實(shí)現(xiàn)模塊化》,本文關(guān)鍵詞  ASP,使用,類,實(shí)現(xiàn),模塊化,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《[ASP]使用類,實(shí)現(xiàn)模塊化》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于[ASP]使用類,實(shí)現(xiàn)模塊化的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    衡南县| 都匀市| 河间市| 南城县| 泸水县| 乌海市| 甘南县| 双桥区| 嘉义市| 章丘市| 监利县| 垦利县| 梧州市| 社会| 虞城县| 三门峡市| 温州市| 泾源县| 永顺县| 自治县| 怀安县| 兴隆县| 启东市| 铁岭县| 松桃| 宁波市| 华池县| 瓮安县| 精河县| 黎川县| 民勤县| 寻乌县| 新宁县| 荆州市| 四子王旗| 伊金霍洛旗| 五家渠市| 大足县| 秦安县| 镇巴县| 茂名市|