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


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