ASP 類程式設計

2021-05-23 10:49:22 字數 4366 閱讀 9805

首先asp的類是由事件和方法(它們就是構成類的成員了)構成的,asp模擬較簡單,它不具有真正意義類的繼承、過載等特性,儘管如此,它仍然是非常有用的,它降低了我們程式設計的複雜度,使我們的程式模組更加合理。 它支援以下內容:

公有成員變數、私有成員變數、屬性

公有成員函式、私有成員函式

建構函式、析構函式

在 class 塊中,成員通過相應的宣告語句被宣告為 private(私有成員,只能在類內部呼叫) 或 public(公有成員,可以在類內外部呼叫) 。被宣告為 private 的將只在 class 塊內是可見的。被宣告為 public 不僅在 class 塊的內部是可見的,對 class 塊之外的**也是可見的。沒有使用 private 或 public 明確宣告的被預設為 public。在類的塊內部被宣告為 public 的過程(sub 或 function)將成為類的方法。public/private 變數將成為類的屬性,同使用 property get、property let 和 property set 顯式宣告的屬性一樣。類的預設屬性和方法是在它們的宣告部分用 default 關鍵字指定的。

注:關於幾個宣告語句

public 宣告公用變數

private 宣告私有變數

property let 是設定變數用的

property set 是設定物件用的

property get 是取回屬性用的

下面是很好的乙個關於類的例子:

'//--------------------------------開始乙個類---------------------------------//

class myclass

'//----宣告(宣告就是定義)myclass類的類內部(私有的[private])變數

private strauthor

private strversion

private strexample

'//---------------------------定義類的事件-------------------------------//

'//----class_initialize()是類的初始化事件,只要一開始使用該類,首先會觸發該部分的執行,下面我們會在該成員中初始化該類的作者和版本以及在螢幕上顯示一下該類已經開始了

'//----該函式為建構函式,在使用 set new 建立物件時,自動執行

private

sub class_initialize(

) strauthor =

"coldstone"

strversion =

"1.0"

response.write

"myclass開始了"

endsub

'//----class_terminate()是類的結束事件,只要一退出該類,就會觸發該事件,下面我們會該事件中設定退出該類時會在螢幕上顯示該類已結束了。

'//----該函式為析構函式,在使用 set nothing 釋放物件時,自動執行

private

sub class_terminate(

)response.write

"myclass結束了"

endsub

'//---------------------------使用者自己定義的方法-------------------------------//

'//----該方法返回乙個版權資訊

public

sub information(

)response.write

endsub

'//---------------------------定義類的輸出屬性-------------------------------//

'//----定類的屬性,該屬性是讓使用者初始化strexapmle變數

'//----property let宣告了當前屬性是:寫屬性

public

property

let setexapmle(

byval strvar)

strexapmle = strvar

endproperty

'//---------------------------定義類的輸出屬性-------------------------------//

'//----定義類的屬性,該屬性是返回乙個版本號

'//----property get宣告了當前屬性是:讀屬性

public

property

get version

version = strversion

endproperty

'//----定義類的屬性,該屬性是返回該類的作者號

public

property

get author

author = strauthor

endproperty

'//----定義類的屬性,該屬性是返回乙個版本號

public

property

get exapmle

exapmle = strexapmle

endproperty

endclass

'//-------這裡是使用該類的例子

dim onenewclass

set onenewclass =

new myclass

response.write

& onenewclass.author

&""response.write

"版本: "

& onenewclass.version

&""onenewclass.setexapmle

="這是乙個簡單類的例子"

response.write

"使用者自定義:"

& onenewclass.exapmle

&""onenewclass.information

set onenewclass =

nothing

asp類的功能實在有限,所以有時我們要實現複雜的功能,只好用組合來實現。

class cfish

sub swim(

)end

subend

class

class cbird

sub fly(

)end

subend

class

class canimal

dim fish

dim bird

private

sub class_initialize(

)set fish =

new cfish

set bird =

new cbird

endsubprivate

sub class_terminate(

)set fish =

nothing

set bird =

nothing

endsub

endclass

dim animal

set animal =

new canimal

call animal.fish.swim()

'魚游call animal.bird.fly()

'鳥飛set animal =

nothing

class class1

private mrstrecordset

public

property

setrecordset

(value)

ifucase

(typename(value))=

"recordset"

then

set mrstrecordset = value

endifend

property

public

function isclose

isclose =

false

if mrstrecordset.state=0

then

isclose =

true

endif

endfunction

endclass

'建立物件

set cls1 =

new class1

'建立rs

set rs =

server.createobject

("adodb.recordset"

)'cls1的recordset屬性引用建立的rs

set cls1.recordset

= rs

'判斷rs是否關閉

response.write cls1.isclose

asp程式設計 筆記

vbscript書冊 option explicit 強制要求顯式宣告指令碼中的所有變數。conn.asp 連線 開啟 資料集的定義 sql select from djcat set rs server.createobject adodb.recordset 也可以使用set rs conn.e...

ASP中使用類

class webuser public property get islogin username if username then islogin true else islogin false jstz 請您先登入 login.htm end if end property public pr...

簡單asp類模仿

現在才發現asp裡面也可以自己寫一些類,而且基本上和其他語言差不多,所以就自己寫了乙個試試,嘿嘿!紀年一下!class clsdsj private p error,p author,p weburl public property get version version dsj 1.0.0 bet...