梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
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�察�regardless if it is possible or not。 If the cast is not possible�察�a
cast exception is thrown。
Another way to perform a type cast is to use a query cast�察�as illustrated by the following
code�察�again assuming the inheritance hierarchy of this section。
Dim backToDerived As Derived = TryCast��baseCls�察�Derived��
In the code�察�the cast involves using the TryCast method。 This cast is a query because a cast
will be attempted。 If the cast is successful�察�then an instance of the type is assigned to the vari
able backToDerived。 If the cast is not possible�察�then backToDerived is assigned a Nothing value。
No exception is thrown。 This casting technique is possible only for reference types。
´´´´´´´´´´´´´´´´´´´´´´Page 217´´´´´´´´´´´´´´´´´´´´´´´
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 195
The Important Stuff to Remember
In this chapter�察�you learned about interfaces and implementations。 Here are the key points
to remember�此�
o Using interfaces is not like using inheritance。 They are two separate designs�察�even though
interfaces will use inheritance。
o Interfaces at an abstract level imply ideas of how you would like your application to work。
o Ideas when implemented as interfaces should be general and applicable to multiple
application implementations for the domain。
o Ideas are represented using Visual Basic interfaces。 And interfaces are implemented
using classes or structures。 But note that interfaces are reference types。
o Factories are used to instantiate implementations and return an instance that imple
ments an interface。 Using a factory means that the user of an interface does not need to
know which type of implementation object is instantiated。
o Interfaces can be considered as facets that might be targeting a specific characteristic of
an implementation。 However�察�as illustrated in the previous chapter�察�interfaces do not
expose Friend state or the Friend workings of the implementations。
o ponents are a fundamental way of developing code and should be your primary
way of writing code。 For the remainder of this book�察�interfaces will be used whenever
possible。 Take notice and try to understand what the idea behind the interface is。
Some Things for You to Do
The following are a couple things for you to do to apply what you¨ve learned in this chapter。
1。 Implement your own tax system using the predefined base classes。
*Note Because of the number of possible tax systems�察�I do not provide an answer for exercise 1。 If you
want me to review your answer�察�please send it to me at christianhgross@gmail。。
2。 Add functionality to the tax engine¨s base classes that implement the behavior of a
minimum tax´free amount。 This means if your ine is not above the minimum tax
free amount�察�you do not pay taxes。
´´´´´´´´´´´´´´´´´´´´´´Page 218´´´´´´´´´´´´´´´´´´´´´´´
´´´´´´´´´´´´´´´´´´´´´´Page 219´´´´´´´´´´´´´´´´´´´´´´´
C H A P T E R 8
* * *
Learning About ponent
Oriented Architecture
So far�察�you have learned the essentials of Visual Basic。 With the essentials�察�you can write a
functional application that uses classes�察�objects�察�interfaces�察�and inheritance。 In this chapter�察 �
you¨ll learn about a Visual Basic programming technique that some developers define as structural。
A structural programming technique is when the code does not directly serve to solve a business
problem�察�but solves a problem relating to building the application。
Another purpose of this chapt