梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
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 _length As Double
Public Property Length�┌� As Double
Get
Return _length
End Get
Set��ByVal value As Double��
_length = value
End Set
End Property
Public Overloads Function CalculateArea�┌� As Double
Return Width * Length
End Function
End Class
Rectangle cannot be described using a single dimension�察�thus we need to add the property
Length。 In the implementation of the Rectangle。CalculateArea�┌� method to calculate the area�察 �
the length is multiplied by the width。
Take a good look at how CalculateArea�┌� is declared。 In the case of Rectangle。
CalculateArea�┌��察�the Overloads keyword is used�察�not Overrides。 This is because you want to
´´´´´´´´´´´´´´´´´´´´´´Page 192´´´´´´´´´´´´´´´´´´´´´´´
170 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
enforce calculation consistency。 Calculation consistency is when you perform a specific calcula
tion on a type and get the answer expected of that type�察�and not some other type。
So�察�say you instantiate Rectangle�察�and then cast it to Square。 When you call CalculateArea�┌��察 �
you want it to calculate as if the rectangle were a square�察�not a rectangle。 Thus�察�by adding the
Overloads keyword in the Rectangle。CalculateArea�┌� method�察�a square is calculated as a
square�察�and a rectangle is calculated as a rectangle。
But there is a consequence。 Let¨s say Rectangle is cast to Shape。 As the inheritance is declared
when calling CalculateArea�┌��察�the area of a square is calculated�察�which is not correct。 Thus it
would seem that using New is incorrect�察�and Overrides should be used instead。 So using Overrides
solves the Shape。CalculateArea�┌� problem�察�but when a rectangle is converted into a square�察 �
the area represents a rectangle and not square。
To illustrate the differences�察�assuming the use of Overloads�察�look at the following source
code�察�which calculates the area of a Rectangle。
Dim cls As Rectangle = New Rectangle�┌�
cls。Width = 20
cls。Length = 30
Dim area As Double = cls。CalculateArea�┌�
In the example�察�Rectangle is instantiated�察�and the properties Width and Length are assigned
values of 20 and 30�察�respectively。 When the CalculateArea�┌� method is called�察�the found area is
assigned to the variable area。
The source code does what we expect。 It instantiates a rectangle�察�assigns the rectangle
dimensions�察�and calculates the area of the rectangle。 But a Rectangle object can also be assigned to
a square variable。 Consider the following modified source code�此�
Dim rectangle As Rectangle = New Rectangle�┌�
rectangle。Width = 20
rectangle。Length = 30
Dim square As Square = rectangle
Dim area As Double = square。CalculateArea�┌�
Console。WriteLine�─�Square Area is ; & square。CalculateArea�┌� & _
; Rectangle Area is ; & rectangle。CalculateArea�┌���
In the example�察�the variable rectangle is of type Rectangle。 The dimensions of the rectangle
are assigned�察�and then the rectangle is converted into a square and assigned to the variable square。
Using the keyword Overloads�察�the area is 400�察�which is correct because when we ask for the
dimensions of the square�察�we get a width of 20。
The various techniques used in this example are explained in the remainder of this chapter。
*Note The example illustrates that by using inheritance�察�you can cast a type and get the appropriate behavior。
But this works only if you design your inheritance hierarchy properly。 You need to understand that behavior
depends on the type that you have from the inheritance tree。 And if you are not careful�察�you can get some very
odd side effects。 Visual Basic allows you to explicitly define what each method does�察�and you should think
very hard about what each method should do。
´´´´´´´´´´´´´´´´´´´´´´Page 193´´´´´´´´´´´´´´´´´´´´´´´
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 171
Illustrating ponents Using a Shape�察�Rectangle�察�and Square
Another way to implement a shape is to use ponents。 Using ponents means to define
an idea�察�and then define an implementation of the idea。 Designing and implementing po
nents is not similar to designing and implementing inheritance trees。 With inheritance�察�you
need to consider type casting�察�base class functionality�察�and how to override or overload methods or
properties。 �─�Type casting is when you cast to a specific type with or without an explicit cast
operator。�� With ponents�察�you need to think in terms of ideas and how they would be imple
mented as interfaces。
Having looked at the Shape�察�Rectangle�察�and Square implementation�察�you might define an
interface ��named IShape�� as follows�此�
Interface IShape
Function CalculateArea�┌� As Double
Property Width�┌� As Double
End Interface
For the IShape declaration�察�you might even add a Length property�察�but the overall idea of
the IShape interface is wrong。 When you think of a shape�察�do you think in terms of length and
width�拭�Probably not。 Rather�察�you think in terms of area�察�perimeter�察�and other features that are
mon to all shapes。 Length and width are not mon to all shapes。 A circle has a radius or
diameter�察�a triangle has base width�察�height�察�and triangle peak offset。