《VB2008从入门到精通(PDF格式英文版)》第93章


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; th
小说推荐
返回首页返回目录