標籤:
=======================================================================
Property Get 語句
在 Class 塊中,聲明構成用來取得(返回)的值的屬性過程的主體的名稱、參數和代碼。
[Public [Default]| Private] Property Get name [(arglist)]
[statements]
[[Set] name = expression]
[Exit Property]
[statements]
[[Set] name = expression]
End Property
參數
Public
表明Property Get 過程可以被所有指令碼中的其他過程訪問。
Default
只與 Public 關鍵字一起使用,表明 Property Get 過程中定義的屬性為類的預設屬性。
Private
表明 Property Get 過程只對定義它的 Class 塊中的其他過程是可以訪問的。
name
Property Get 過程的名稱;遵守標準的變數命名規則,區別僅僅在於它可以與同一Class塊中的Property Let 或 Property Set過程。
arglist
該變數列表代表了 Property Get 過程被調用時傳遞給它的參數。多個參數之間用逗號分隔開。Property Get 過程中的每個參數的名稱必須與 Property Let 過程中的相應參數相同(如果有的話)。
statements
任意的一組語句,將在 Property Get 過程的主體中執行。
Set
在將對象作為 Property Get 過程的傳回值時使用的關鍵字。
expression
Property Get 過程的傳回值。
說明
如果未使用 Public 或 Private明確聲明,則 Property Get 過程被預設為公有的,即它們對於指令碼中的其他所有過程都是可見的。Property Get過程中的局部變數的值在不同的程序呼叫之間是不儲存的。
在其他任何過程(例如 Function 或 Property Let) 內部都不能定義 Property Get 過程。
Exit Property 語句將導致立即從 Property Get 過程中退出。程式將繼續執行調用 Property Get 過程的語句之後的程式。Exit Property 語句可以出現在 Property Get 過程中的任何位置,次數不限。
與Sub 和 Property Let 過程類似,Property Get 過程是能夠接受參數的過程,可以執行一系列的語句,以及改變參數的值。然而,與Sub 和 Property Let 不同的是,Property Get 過程可以被用於運算式的右側,用與使用Function 或屬性名稱相同的方式來返回屬性的值。
例子:
Class myClass1
Public a1,a2
Public Property Get a3(ByVal a5)
a3=a5&" good"
End Property
End Class
Dim x
Set x=New myClass1
response.Write("x.a1+x.a2="&x.a3("中國")&"〈br〉")
x.a1=1
x.a2=2
Response.Write("x.a1="&x.a1&"〈br〉")
Response.Write("x.a2="&x.a2&"〈br〉")
運行結果為:
x.a1+x.a2=中國 good
x.a1=1
x.a2=2
對屬性唯讀
==============================================================================
Property Let 語句
在 Class 塊中,聲明名稱、參數和代碼等,它們構成了賦值(設定)的 Property 過程的主體。
[Public | Private] Property Let name ([arglist,] value
)
[statement]
[Exit Property]
[statement]
End Property
參數
Public
表明 Property Let 過程可以被所有指令碼中的其他所有過程訪問。
Private
表明 Property Let 過程只能被定義之的 Class 塊內的其他過程訪問。
name
Property Let 過程的名稱;遵守標準的變數命名規則,區別僅僅在於其名稱可以與相同 Class 塊中的 Property Get 或 Property Set 過程相同。
arglist
該變數列表代表了在調用時被傳遞到 Property Let 過程的參數。多個參數之間用逗號隔開。Property Let 過程的每個參數的名字必須與 Property Get 過程中的相應參數相同。此外, Property Let 過程的參數比相應的 Property Get 過程總要多一個。該參數為被賦予屬性的值。
value
該變數中包含要賦予屬性的值。當過程被調用時,該參數將出現在調用運算式的右側。
statement
任意的一組語句,將在 Property Let 過程的主體內執行。
注意每個 Property Let 語句必須為所定義的屬性定義至少一個參數。該參數(在存在多個參數時的最後一個參數)包含了當 Property Let 語句被調用時要賦予屬性的值。該參數在前面的文法中被稱為value。
說明
如果未明確地使用 Public 或 Private進行指定,Property Let 過程被預設設定為公有的,即它們對於指令碼內的其他所有過程都是可見的。Property Let過程中的局部變數的值在不同的程序呼叫之間是不被儲存的。
在其他任何過程(例如 Function 或 Property Get)的內部不能夠定義 Property Let 過程。
Exit Property 語句將導致立即從 Property Let 過程中退出。程式將從調用 Property Let 過程的語句之後的點繼續執行。Exit Property 語句可以出現在 Property Let 過程中的任何位置,次數不限。
與 Function 和 Property Get 過程類似,Property Let 過程是一個單獨的過程,它可以接受參數,執行一系列的語句,還可以改變參數的值。不過,與Function 和 Property Get 過程不同的是,它們兩者都返回一個值,而Property Let過程只能用於屬性賦值運算式的左側。
例子:
Class myClass1
Public a1,a2
Public Property Let a3(ByVal a5)
a3=a5 & " good"
a1=a5
End Property
End Class
Dim x
Set x=New myClass1
x.a3=8
response.Write("x.a1+x.a2="&x.a1&"〈br〉")
x.a1=1
x.a2=2
Response.Write("x.a1="&x.a1&"〈br〉")
Response.Write("x.a2="&x.a2&"〈br〉")
運行結果為:
x.a1+x.a2=8
x.a1=1
x.a2=2
對屬性唯寫,必須有一個參數用於接受外來的賦值
===================================================================================
Property Set 語句
在 Class 塊中,聲明名稱、參數和代碼,這些構成了將引用設定到對象的 Property 過程的主體。
[Public | Private] Property Set name(
[arglist,] reference
)
[statement]
[Exit Property]
[statement]
End Property
參數
Public
表明 Property Set 過程可以被所有指令碼中的其他所有過程訪問。
Private
表明 Property Set 過程只能被聲明之的同一 Class 塊中的其他過程訪問。
name
Property Set 過程的名稱;遵守標準的變數命名規則,不過該名稱可以與同一個 Class 塊中的 Property Get 或 Property Let 過程相同。
arglist
變數列表,代表在 Property Set 過程被調用時傳遞給它的參數。多個參數之間用逗號分隔開。此外,Property Set 過程將總是比它相應的 Property Get 過程多一個參數。這個多出來的參數是被賦予的對象。
reference
變數,其中包含被用於對象引用賦值的右側的對象引用。
statement
將要在 Property Set 過程主體中執行的任何一組語句。
注意每個 Property Set 語句都必須為所已定義流程定義至少一個參數。當 Property Set 語句所已定義流程被調用時,該必需參數(在多個參數時為最後一個參數)中將為屬性提供實際的對象引用。在前面的文法中,該參數被稱為引用。
說明
除非使用 Public 或 Private 明確地進行指定,否則 Property Set 過程被設定為預設的公有模式,即指令碼中的其他所有過程都是可見的。在不同的程序呼叫時,Property Set 過程中的局部變數是不儲存的。
在其他任何過程(例如Function或Property Let)中都不能夠定義Property Set過程。
Exit Property 語句將導致立即從 Property Set 過程中退出。程式將繼續執行調用 Property Set 過程之後的語句。Exit Property 語句的數量不受限制,可以出現在 Property Set 過程中的任何位置。
與 Function 和 Property Get 過程類似,Property Set 過程是一個單獨的過程,它可以有若干參數,執行一系列的語句,還可以改變參數的值。然而,與 Function 和 Property Get 過程不同的是,函數和過程都可以傳回值,而 Property Set 過程對象引用賦值(Set 語句)的左側。
例子:
Class myClass1
Public a1,a2
Private a5
Public Property Set a3(reference)
Dim conn,connStr
connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("count.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connStr
reference.open "SELECT * FROM wenzi",conn,1,1
a5=reference.recordcount
conn.Close
Set conn=Nothing
End Property
Public Property Get a3()
a3=a5
End Property
End Class
Dim x
Set x=New myClass1
Set x.a3=server.CreateObject("ADODB.Recordset")
response.Write("x.a1+x.a2="&x.a3&"〈br〉")
x.a1=1
x.a2=2
Response.Write("x.a1="&x.a1&"〈br〉")
Response.Write("x.a2="&x.a2&"〈br〉")
運行結果:
x.a1+x.a2=21900
x.a1=1
x.a2=2
asp:Property解釋與例子