1)在fuction中修飾區域變數,讓這個變數的value不會在離開function後就從記憶體中消失
也就是可以保留前一次function留下的"痕跡"
2)在class定義中修飾class member,讓這個member由該class的所有instance共用
但是在VB.Net這兩個功能被拆成兩個修飾詞,分別是"Static"與"Shared"
以下簡述它的特性
Static:
MSDN說明:Normally, a local variable in a procedure ceases to exist as soon as the procedure stops. A static variable continues to exist and retains its most recent value. The next time your code calls the procedure, the variable is not reinitialized, and it still holds the latest value that you assigned to it. A static variable continues to exist for the lifetime of the class or module that it is defined in.
規則:
1.Static只能修飾Local Variable(函式內的變數)
2.不能修飾Struct的成員函式
3.不能與ReadOnly, Shadows, Shared同時使用
舉例:
Function updateSales(ByVal thisSale As Decimal) As Decimal
Static totalSales As Decimal = 0
totalSales += thisSale
Return totalSales
End Function
因為Satic所以只有第一次進來function才會初始化
所以totalSales不會被歸零
Shared:
MSDN說明:Sharing a member of a class or structure makes it available to every instance, rather than nonshared, where each instance keeps its own copy. This is useful, for example, if the value of a variable applies to the entire application. If you declare that variable to be Shared, then all instances access the same storage location, and if one instance changes the variable's value, all instances access the updated value.
Sharing does not alter the access level of a member. For example, a class member can be shared and private (accessible only from within the class), or nonshared and public.
規則:
1.Shared只能修飾Struct or Class的member(方法或變數)
2.不能與 Overrides, Overridable, NotOverridable, MustOverride, Static 同時使用
3.建議以Struct或Class名稱操作Shared變數,如:Class1.SharedVal1 += 3
(如果用instance操作也可以,不過會有warnning! 這個與C++不同)
4.Shared variable只會存有一份,不管物件有多少個,而Shared function的local variable也是!
(也就是Shared function的local variable隱含的是Shared)
5.不能修飾module與interface的成員、Constant變數,但是他們仍是Shared
6.Shared函式不能使用non-shared變數,只能用shared變數。
但是nonshared函式卻可以使用non-shared、shared變數
(因為non-shared變數是每個instance都有一份專屬的,Shared函式根本不知道你要使用誰的!)
舉例:
Sub main()
shareTotal.total = 10
' The preceding line is the preferred way to access total.
Dim instanceVar As New shareTotal
instanceVar.total += 100
' The preceding line generates a compiler warning message and
' accesses total through class shareTotal instead of through
' the variable instanceVar. This works as expected and adds
' 100 to total.
returnClass().total += 1000
' The preceding line generates a compiler warning message and
' accesses total through class shareTotal instead of calling
' returnClass(). This adds 1000 to total but does not work as
' expected, because the MsgBox in returnClass() does not run.
MsgBox("Value of total is " & CStr(shareTotal.total))
End Sub
Public Function returnClass() As shareTotal
MsgBox("Function returnClass() called")
Return New shareTotal
End Function
Public Class shareTotal
Public Shared total As Integer
End Class
注意:即使returnClass()會回傳shareTotal的物件
但是實際上complier遇到這類的shared操作手法
會直接用shareTotal.total+=1000來解讀
也就是他根本不會進去returnClass()!!!!
沒有留言:
張貼留言