梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
Class Derived
Inherits Base
Public Overloads Sub Method�┌�
Console。WriteLine�─�Derived。Method;��
End Sub
End Class
´´´´´´´´´´´´´´´´´´´´´´Page 211´´´´´´´´´´´´´´´´´´´´´´´
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 189
Module Test
Public Sub Run�┌�
Dim derivedCls As Derived = New Derived�┌�
Dim baseCls As Base = derivedCls
' Calls Derived。Method
derivedCls。Method�┌�
' Calls Base。Method
baseCls。Method�┌�
End Sub
End Module
o Keywords used�此�Overloads in the derived class to indicate the method is being overloaded。
o Overloading a method means to change the functionality of the method in the derived class。
o Which method is called in the inheritance depends on the type of the variable on which
the method is called。 Thus�察�if the variable is of type Base�察�Base。Method�┌� is called�察�if the
variable is of type Derived�察�Derived。Method�┌� is called。
Scenario 2�此�Overriding Base Class Functionality
Class Base
Public Overridable Sub Method�┌�
Console。WriteLine�─�Base。Method;��
End Sub
End Class
Class Derived
Inherits Base
Public Overrides Sub Method�┌�
Console。WriteLine�─�Derived。Method;��
End Sub
End Class
Module Test
Public Sub Run�┌�
Dim derivedCls As Derived = New Derived�┌�
Dim baseCls As Base = derivedCls
' Calls Derived。Method
derivedCls。Method�┌�
' Calls Derived。Method
baseCls。Method�┌�
End Sub
End Module
´´´´´´´´´´´´´´´´´´´´´´Page 212´´´´´´´´´´´´´´´´´´´´´´´
190 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
o Keywords used�此�Overridable in the base class to indicate the method behavior can be
changed in the derived class。 Overrides is used in the derived classes to indicate a method
with the new behavior。 Multiple levels of inheritance require multiple usages of Overrides。
o Overriding a method means to change the behavior of the method in the base class to
that of the derived class。 If there are multiple levels of inheritance�察�then the functionality
used is the instantiated type。
o You can also define a Overridable base class method using the MustOverride keyword。
The difference between Overridable and MustOverride is that Overridable has a method
or property implementation in the base class�察�whereas MustOverride has no implemen
tation in the base class。
Scenario 3�此�Implementing an Interface
Interface IInterface
Sub Method�┌�
End Interface
Class Implementation
Implements IInterface
Public Sub Method�┌� Implements IInterface。Method
Console。WriteLine�─�Implementation。Method;��
End Sub
End Class
Module Test
Public Sub Run�┌�
Dim implementation As Implementation = New Implementation�┌�
Dim inst As IInterface = implementation
' Calls Implementation。Method
implementation。Method�┌�
' Calls Implementation。Method
inst。Method�┌�
End Sub
End Module
o Keywords used�此�none。
o The class that implements the interface has a behavior�察�like when a class subclasses and
implements a MustOverride method。
o Whether the variable is an interface type or an implementation type�察�the method
Implementation。Method�┌� is called。
´´´´´´´´´´´´´´´´´´´´´´Page 213´´´´´´´´´´´´´´´´´´´´´´´
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 191
Scenario 4�此�Implementing Two Interfaces with Same Method/Property Name
Interface IInterface1
Sub Method�┌�
End Interface
Interface IInterface2
Sub Method�┌�
End Interface
Class Implementation
Implements IInterface1�察�IInterface2
Public Sub Method1�┌� Implements IInterface1。Method
Console。WriteLine�─�Implementation。IInterface1。Method;��
End Sub
Private Sub Method2�┌� Implements IInterface2。Method
Console。WriteLine�─�Implementation。IInterface2。Method;��
End Sub
End Class
Module Test
Public Sub Run�┌�
Dim implementation As Implementation = New Implementation�┌�
Dim inst1 As IInterface1 = implementation
Dim inst2 As IInterface2 = implementation
' Calls Implementation。Method1
inst1。Method�┌�
' Calls Implementation。Method2
inst2。Method�┌�
End Sub
End Module
o Keywords used�此�special notation for the implementation of a particular interface method
��for example�察�Implements IInterface1。Method and Implements IInterface2。Method��。
o The methods that are converted to implement a particular interface can still be called。
In the example�察�Method1�┌� is declared as Public�察�and thus if you have a reference to
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 puzz