梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
Understanding Static Data Members and Methods
You¨ve seen how a constructor can be used to initialize the state of a particular instance of a
type。 Now we need to define a constructor for the tree structure shown in Figure 4´2。 A tree
implies a starting point�察�but the code for the flight connections does not imply a single starting
point。 Instead�察�we have the declaration of a number of variables where the identifier of each
variable is a city。
The problem with such a declaration is that�察�if you want to navigate a tree structure�察�you
need to know the individual names of the variables and navigate the tree structure of each vari
able。 This is not a plausible solution。 You want to create a single overall point from where all
other cities can be referenced。
The solution lies in using an array like that used for the data member Connections。 To address
the problem of providing a single access point�察�we declare a shared data member�察�as follows�此�
Public Class Node
Public Shared RootNodes As Node�┌�
Public CityName As String
Public X As Double
Public Y As Double
Public Connections As Node�┌�
Public Sub New��string city�察�double x�察�double y��
Me。CityName = city
Me。X = x
Me。Y = y
Me。Connections = Nothing
End Sub
End Class
The bold code shows the declaration of a data member with the modifier Shared to indi
cate that the data member is shared。 So�察�just what does shared mean in this context�拭�
Let¨s say that your family consists of your significant other and two kids。 One day�察�you
wander into the cell phone store and decide to buy four identical cell phones�察�as the store is
having a family deal。 When each phone is activated�察�the state of each cell phone will be unique。
Each person will have an individual number�察�address book�察�and so on。 In the analogy to objects�察 �
the cell phone represents a type�察�each person is an instance of the same type�察�but with a unique
state that represents an instantiation of the type。
Some cell phones have a feature called push´to´talk。 Essentially�察�you convert a cell phone
into a walkie´talkie。 After buying individual cell phones for the family�察�the push´to´talk feature is
activated for everyone in the family。 This means if one person is using the push´to´talk feature�察 �
all family members will hear the conversation。 The push´to´talk feature does not distinguish
between the cell phones�察�so if multiple people talk at the same time�察�so be it�察�you¨ll hear quite a
bit of noise。 Push´to´talk is a shared resource�察�not associated with a particular cell phone。 In the
same way�察�Shared refers to a shared resource that is not associated with a particular type instance。
´´´´´´´´´´´´´´´´´´´´´´Page 119´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 4 * L E A R N I N G A B OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S 97
When you associate Shared with a data member�察�as in the code example you just saw�察�you
are saying that�察�regardless of how many times you instantiate Node�察�there is always a single instance
of the data member RootNodes。 You don¨t even need to instantiate Node to access RootNodes。 Shared
methods are like shared data members�察�in that they are a shared resource and are not associated
with a particular object ��as illustrated by the Main�┌� method used to start an application��。
Figure 4´14 illustrates what you can and cannot do with shared and object instance data
members。
Class MySharedAndNonSharedClass Assignment allowed because shared
Public Shared value As Integer can reference shared
Public instanceValue As Integer
Public Shared Sub MyMethod�┌� Assignment not allowed because shared cannot
value = 10
instanceValue = 20 reference information that is not shared�察�unless the
End Sub containing type is instantiated with the New keyword
Public Sub MyInstanceMethod�┌�
value = 10 Assignment allowed because instance can reference shared
instanceValue = 20
End Sub
End Class Assignment allowed because
instance can reference instance
Class TestSharedVsNonShared Method call allowed because shared
Public Sub TestSimple�┌� means you do not need to instantiate
MySharedAndNonSharedClass。MyMethod�┌� the type using New keyword
MySharedAndNonSharedClass。MyInstanceMethod�┌�
Dim cls As MySharedAndNonSharedClass = New MySharedAndNonSharedClass�┌�
cls。MyInstanceMethod�┌�
End Sub
End Class Method call not allowed because method
can be called only when the type is
instantiated using New keyword
Figure 4´14。 Examples of shared and object instance data members
The general rule of thumb is that shared data members or methods can be accessed without
having to instantiate the type。 Also�察�don¨t attempt to reference object instance data members
or methods in a shared method。
Getting back to the Node declaration�察�the shared data member RootNodes is used to define
a root for the search tree。 As when instantiating a type�察�there is a constructor for the shared type
that is called before the first time it is needed。 The shared constructor is like the previously
defined constructor�察�except the Public keyword is replaced with Shared。 For the search tree
case�察�it is used to initialize the tree and state。
We now have a plete definition of the Node class�察�with the following source code。 Take
a moment to look it over and fit the pieces together。
Public Class Node
Shared Sub New�┌�
Dim montreal As Node = New Node�─�Montreal;�察�0�察�0��
Dim newyork As Node = New Node�─�New York;�察�0�察 �3��
Dim miami As Node = New Node�─�Miami;�察 �1�察 �11��
Dim toronto As Node = New Node�─�Toronto;�察 �4�察 �1��
Dim houston As Node = New Node�─�Houston;�察 �10�察 �9��
Dim losangeles As Node = New Node�─�Los Angeles;�察 �17�察 �6��
´´´´´´´´´´´´´´´´´´´´´´Page 120´´´´´´´´´´´´´´´´´´´´´´´
98 CH AP T E R 4 * L E A R N IN G AB OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S
Dim seattle As Node = New Node�─�Seattle;�察 �16�察 �1��
montreal。Connections = New Node�┌� ��newyork�察�toronto�察�losangeles��
newyork。Connections = New Node�┌� ��montreal�察�houston�察�miami��
miami。Connections = New Node�┌� ��toronto�察�houston�察�newyork��
toronto。Connections = New Node�┌� ��miami�察�seattle�察�montreal��
houston。Connections = New Node�┌� ��miami�察�seattle�察�newyork��
seattle。Connections = New Node�┌� ��toronto�察�houston�察�losangeles��
losangeles。Connections = New Node�┌� ��montreal�察�seattle�察�houston��
Node。RootNodes = New Node�┌� ��montreal�察�newyork�察�miami�察�toronto�察�houston�察�_