梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
Implementation�察�then you can call Method1�┌�。
Scenario 5�此�Implementing an Interface That Allows Overriding
Interface IInterface
Sub Method�┌�
End Interface
´´´´´´´´´´´´´´´´´´´´´´Page 214´´´´´´´´´´´´´´´´´´´´´´´
192 CH AP T E R 7 * L E A R N IN G AB OU T CO M P O N E N TS AN D C L AS S H I E R AR C H IE S
Class Implementation
Implements IInterface
Public Overridable Sub Method�┌� Implements IInterface。Method
Console。WriteLine�─�Implementation。Method;��
End Sub
End Class
Class ImplementationDerived
Inherits Implementation
Public Overrides Sub Method�┌�
Console。WriteLine�─�ImplementationDerived。Method;��
End Sub
End Class
Module Test
Public Sub Run�┌�
Dim implementation As ImplementationDerived = _
New ImplementationDerived�┌�
Dim inst As IInterface = implementation
' Calls ImplementationDerived。Method
implementation。Method�┌�
' Calls ImplementationDerived。Method
inst。Method�┌�
End Sub
End Module
o Keywords used�此�Overridable and Overrides。
o By default�察�when you implement an interface without using the Overridable keyword�察 �
you are saying that the derived class can overload only the method。 In many cases�察�this
is appropriate�察�as you will want to override the method and thus will need to use the
Overridable and Overrides keywords。
o This is a mon problem�察�and most beginning Visual Basic programmers are puzzled
by the overloading behavior。
Scenario 6�此�An Inheritance Tree That Overrides and Overloads
Class Base
Public Overridable Sub Method�┌�
Console。WriteLine�─�Base。Method;��
End Sub
End Class
´´´´´´´´´´´´´´´´´´´´´´Page 215´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 7 * L E AR N IN G AB O U T CO M P O N E N TS AN D C L AS S H I E R AR C HI E S 193
Class Derived1
Inherits Base
Public Overrides Sub Method�┌�
Console。WriteLine�─�Derived1。Method;��
End Sub
End Class
Class Derived2
Inherits Derived1
Public Overloads Overridable Sub Method�┌�
Console。WriteLine�─�Derived2。Method;��
End Sub
End Class
Class Derived3
Inherits Derived2
Public Overrides Sub Method�┌�
Console。WriteLine�─�Derived3。Method;��
End Sub
End Class
Module Test
Public Sub Run�┌�
Dim derivedCls As Derived3 = new Derived3�┌�
Dim baseCls As Base = derivedCls
Dim derived2cls As Derived2 = derivedCls
' Calls Derived3。Method
derivedCls。Method�┌�
' Calls Derived1。Method
baseCls。Method�┌�
' Calls Derived3。Method
derived2cls。Method�┌�
End Sub
End Module
o Keywords used�此�Overridable�察�Overrides�察�and Overloads。
o The inheritance hierarchy is saying that Derived1。Method�┌� overrides the behavior of
Base。Method�┌�。 Derived2。Method�┌� overloads the behavior of Derived1。Method�┌��察�while
establishing a new overriding base method。 Derived3。Method�┌� overrides the behavior
Derived2。Method�┌��察�and not Base。Method�┌� ��very important��。
o When confronted with a plex inheritance hierarchy as illustrated by scenario 6�察�it is
important that you start at the base class and work up the inheritance hierarchy。
´´´´´´´´´´´´´´´´´´´´´´Page 216´´´´´´´´´´´´´´´´´´´´´´´
194 CH AP T E R 7 * L E A R N IN G AB OU T CO M P O N E N TS AN D C L AS S H I E R AR C H IE S
More Type´Casting Details
This chapter illustrated some type´casting examples。 In Visual Basic�察�you have two ways to
type cast�此�
o A forced type cast�察�which can also be used on value types
o A type cast that queries if a type cast is possible
Consider this hierarchy�此�
Class Base
Public Sub Method�┌�
Console。WriteLine�─�Base。Method;��
End Sub
End Class
Class Derived
Inherits Base
Public Overloads Sub Method�┌�
Console。WriteLine�─�Derived。Method;��
End Sub
End Class
The next step is to instantiate the type Derived and cast the instance to the base type�此�
Dim derivedCls As Derived = New Derived�┌�
Dim baseCls As Base = derivedCls
When casting from a derived type to a base class type�察�a type cast is not necessary and you
can assume it is implied。
A type cast is necessary when you want to cast a base class instance to a derived class instance。
Following is the source code for a forced type cast�察�assuming the inheritance hierarchy from
the previous cast。
Dim backToDerived As Derived = DirectCast��baseCls�察�Derived��
The forced cast is the use of the method DirectCase。 The cast is forced because a conver
sion to the desired type will occur�察�r