按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
cls。Method()
In the example; the class ExampleImplementation is instantiated and assigned to a variable
cls of type IExample。 Think of it as saying; “I am instantiating ExampleImplementation and
assigning the instance to the type IExample that is in my inheritance hierarchy。” When the
method cls。Method() is called; the caller is really calling ExampleImplementation。Method();
although the caller would not know that; as it is using the base type IExample。
The mechanics of defining an interface and its associated implementation are straightfor
ward; but why would you do it? To explain; I’ll use a real…life analogy。
Let’s say that you are going to a restaurant for an evening of fine dining。 When you are
sitting at the table; you expect a waiter to take your order; serve your food; remove the used
dishes; and give you the bill。 All of these actions are ideas used to run a restaurant that serves a
…………………………………………………………Page 190……………………………………………………………
168 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
clientele。 You make use of the idea; as do millions of other clients。 The idea is applied across all
restaurants。
Think about this a bit。 The idea is a waiter; but the implementation is a human。 When you
sit down; your waiter may introduce himself by his name; but do you ever use the name? Most
people don’t; because they think of the waiter as a human that takes their order; serves their
food; and so on。 The point is that the human is the implementation; but you really only care
about a waiter fulfilling the scope of the task。 For example; while you might be sympathetic that
your waiter is having a bad day; you want him to be chipper and happy; and to do his job of
taking your order; serving your food; and so on。 Bluntly put; you probably would not care if the
waiter’s puppy had been run over by a truck。
This is exactly what interfaces and implementations are about。 The interface defines a role
of tasks that an implementation is supposed to implement。 You don’t care if the implementa
tion is capable of doing more; or if the implementation is “having a bad day。” All you care about
is that the implementation does what the interface says it should do。
What you have is a decoupling of the idea from the interface; just like at the restaurant;
where you don’t care if Tom; Dick; Mary; or Jane is your server。 In fact; would you care if the
human waiter were replaced by a robot? Probably not; because what you really care about is
eating the food。 This is an important aspect of interfaces and implementations; in that imple
mentations are replaceable; and you want to be able to swap one implementation for another。
When you use interfaces and types that implement interfaces; you are writing ponent
oriented software。 ponents and inheritance are two different object…oriented techniques。
You use inheritance to implement interfaces; but ponents serve the purpose of making
ideas happen。
Understanding How Inheritance and ponents Work
Inheritance is the act of defining base classes with functionality that may or may not be over
ridden or overloaded; as explained in the previous chapter。 ponents define subsystems
that are put together like pieces of a puzzle。 The idea behind ponents is to be able to asso
ciate two interface instances and make them work with each other without knowing what the
other does。
To get a feeling of the difference between inheritance using classes and ponents that
use interfaces and classes; we will look at a classic example of inheritance and how that example
translates to ponents。
Illustrating Inheritance Using a Shape; Rectangle; and Square
One of the most popular examples of using inheritance involves shapes and how to calculate
the area of a shape。 The starting point of this inheritance is a MustInherit base class that has a
single property and method to indicate a single dimension and its associated area。 For example;
the following would be an appropriate base class definition。
MustInherit Class Shape
Public MustOverride Function CalculateArea() As Double
End Class
The method CalculateArea() is used to calculate the area of the shape。 It is declared as
MustOverride and must be implemented by a derived class。
…………………………………………………………Page 191……………………………………………………………
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 169
For starters; let’s define Square; which represents the square shape。
Class Square
Inherits Shape
Private _width As Double
Public Property Width() As Double
Get
Return _width
End Get
Set(ByVal value As Double)
_width = value
End Set
End Property
Public Overrides Function CalculateArea() As Double
Return Width * Width
End Function
End Class
A square has only one dimension; Width; which represents the width of a particular shape。 In
the case of a square; width means one of the four sides。 We’ve implemented the CalculateArea()
method; which calculates the surface area of the square by multiplying the Width property
by itself。
A rectangle is a form of square; and therefore Rectangle derives from Square:
Class Rectangle
Inherits Square
Private