Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving Inleiding Basisklasses en Afgeleide Klasses Protected en Friend Members Relatie tussen Basisklasses en Afgeleide Klasses Constructors en Finalizers in Afgeleide Klasses Impliciete conversie van Afgeleide-Klasse-Object naar Basis-Klasse-Object Software Engineering met Overerving Compositie vs. Overerving Case Study: Point, Circle, Cylinder Visuele Overerving Visual Basic.NET
6.1 Inleiding Enkele voorbeelden van overerving Visual Basic.NET
6.2 Basisklasses en afgeleide klasses CommunityMember Employee Student Alumnus Faculty Staff Administrator Teacher Overervingshierarchie voor CCommunityMembers (universiteitsgemeenschap) Visual Basic.NET
6.3 Protected en Friend members CShape CTwoDimensionalShape CThreeDimensionalShape CCircle CSquare CTriangle CSphere CCube CCylinder Gedeelte van een CShape klassenhierarchie Visual Basic.NET
6.4 Relatie tussen basisklasses en afgeleide klasses Structurele Overerving Basisklasse De basisklasse moet gedeclareerd worden als “overridable” als een bepaalde methode moeten worden overridden in de afgeleide klasse De basisklasse moet in de mogelijkheid zijn om zijn implementatie vrij te veranderen Derived Class Een object van de afgeleide klasse kan een illegale waarde toekennen aan de Protected data, zodat het object in een incosistente staat komt Afgeleide klasses zouden alleen mogen afhangen van de services (methodes en properties die niet Private zijn) van de basisklasse Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.4: Point.vb 2 ' CPoint class represents an x-y coordinate pair. 3 4 Public Class CPoint 5 ' implicitly Inherits Object 6 7 ' point coordinate 8 Private mX, mY As Integer 9 10 ' default constructor 11 Public Sub New() 12 13 ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 End Sub ' New 17 18 ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) 21 22 ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 End Sub ' New 26 27 ' property X 28 Public Property X() As Integer 29 30 Get 31 Return mX 32 End Get 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 34 Set(ByVal xValue As Integer) 35 mX = xValue ' no need for validation 36 End Set 37 38 End Property ' X 39 40 ' property Y 41 Public Property Y() As Integer 42 43 Get 44 Return mY 45 End Get 46 47 Set(ByVal yValue As Integer) 48 mY = yValue ' no need for validation 49 End Set 50 51 End Property ' Y 52 53 ' return String representation of CPoint 54 Public Overrides Function ToString() As String 55 Return "[" & mX & ", " & mY & "]" 56 End Function ' ToString 57 58 End Class ' CPoint Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.12: Circle4.vb 2 ' CCircle4 class that inherits from class CPoint. 3 4 Public Class CCircle4 5 Inherits CPoint ' CCircle4 Inherits from class CPoint 6 7 Private mRadius As Double 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New 15 16 ' constructor 17 Public Sub New(ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double) 19 20 ' use MyBase reference to CPoint constructor explicitly 21 MyBase.New(xValue, yValue) 22 Radius = radiusValue 23 End Sub ' New 24 25 ' property Radius 26 Public Property Radius() As Double 27 28 Get 29 Return mRadius 30 End Get 31 32 Set(ByVal radiusValue As Double) 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 34 If radiusValue > 0 35 mRadius = radiusValue 36 End If 37 38 End Set 39 40 End Property ' Radius 41 42 ' calculate CCircle diameter 43 Public Function Diameter() As Double 44 Return mRadius * 2 45 End Function ' Diameter 46 47 ' calculate CCircle4 circumference 48 Public Function Circumference() As Double 49 Return Math.PI * Diameter() 50 End Function ' Circumference 51 52 ' calculate CCircle4 area 53 Public Overridable Function Area() As Double 54 Return Math.PI * mRadius ^ 2 55 End Function ' Area 56 57 ' return String representation of CCircle4 58 Public Overrides Function ToString() As String 59 60 ' use MyBase reference to return CPoint String representation 61 Return "Center= " & MyBase.ToString() & _ 62 "; Radius = " & mRadius 63 End Function ' ToString 64 65 End Class ' CCircle4 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.13: CircleTest4.vb 2 ' Testing class CCircle4. 3 4 Imports System.Windows.Forms 5 6 Module modCircleTest4 7 8 Sub Main() 9 Dim circle As CCircle4 10 Dim output As String 11 12 circle = New CCircle4(37, 43, 2.5) ' instantiate CCircle4 13 14 ' get CCircle4's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius 18 19 ' set CCircle4's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = 4.25 23 24 ' display CCircle4's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf 28 29 ' display CCircle4's diameter 30 output &= "Diameter is " & _ 31 String.Format("{0:F}", circle.Diameter()) & vbCrLf 32 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 33 ' display CCircle4's circumference 34 output &= "Circumference is " & _ 35 String.Format("{0:F}", circle.Circumference()) & vbCrLf 36 37 ' display CCircle4's area 38 output &= "Area is " & String.Format("{0:F}", circle.Area()) 39 40 MessageBox.Show(output, "Demonstrating Class CCircle4") 41 End Sub ' Main 42 43 End Module ' modCircleTest4 Class CTime inherits existing pieces of class Object. Visual Basic.NET
6.5 Hiërarchie met overerving op 3 niveaus Voorbeeld van overerving: Point-Circle-Cylinder Point Klasse CPoint bevat instantievariabelen die Private toegankelijk zijn Ze bevat ook properties X en Y waardoor toegang kan verkregen worden tot mX en mY en methode ToString Circle Klasse CCircle4 bevat de functionaliteit van CPoint Ze bevat ook de property voor Radius en de methodes Diameter, Circumference, Area, en ToString Cylinder Klasse CCylinder bevat de functionaliteit van CCircle Ze bevat ook indirect de klasse CPoint en heeft een CCylinder constructor methode, property Height en methode Volume Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.14: Cylinder.vb 2 ' CCylinder class inherits from class CCircle4. 3 4 Public Class CCylinder 5 Inherits CCircle4 6 7 Protected mHeight As Double 8 9 ' default constructor 10 Public Sub New() 11 Height = 0 12 End Sub ' New 13 14 ' four-argument constructor 15 Public Sub New(ByVal xValue As Integer, _ 16 ByVal yValue As Integer, ByVal radiusValue As Double, _ 17 ByVal heightValue As Double) 18 19 ' explicit call to CCircle4 constructor 20 MyBase.New(xValue, yValue, radiusValue) 21 Height = heightValue ' set CCylinder height 22 End Sub ' New 23 24 ' property Height 25 Public Property Height() As Double 26 27 Get 28 Return mHeight 29 End Get 30 31 ' set CCylinder height if argument value is positive 32 Set(ByVal heightValue As Double) 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 34 If heightValue >= 0 Then 35 mHeight = heightValue 36 End If 37 38 End Set 39 40 End Property ' Height 41 42 ' override method Area to calculate CCylinder area 43 Public Overrides Function Area() As Double 44 Return 2 * MyBase.Area + MyBase.Circumference * mHeight 45 End Function ' Area 46 47 ' calculate CCylinder volume 48 Public Function Volume() As Double 49 Return MyBase.Area * mHeight 50 End Function ' Volume 51 52 ' convert CCylinder to String 53 Public Overrides Function ToString() As String 54 Return MyBase.ToString() & "; Height = " & mHeight 55 End Function ' ToString 56 57 End Class ' CCylinder Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.15: CylinderTest.vb 2 ' Tests class CCylinder. 3 4 Imports System.Windows.Forms 5 6 Module modCylinderTest 7 8 Sub Main() 9 10 ' instantiate object of class CCylinder 11 Dim cylinder As New CCylinder(12, 23, 2.5, 5.7) 12 Dim output As String 13 14 ' properties get initial x-y coordinate, radius and height 15 output = "X coordinate is " & cylinder.X & vbCrLf & _ 16 "Y coordinate is " & cylinder.Y & vbCrLf & "Radius is " & _ 17 cylinder.Radius & vbCrLf & "Height is " & cylinder.Height 18 19 ' properties set new x-y coordinate, radius and height 20 cylinder.X = 2 21 cylinder.Y = 2 22 cylinder.Height = 10 23 cylinder.Radius = 4.25 24 25 ' get new x-y coordinate and radius 26 output &= vbCrLf & vbCrLf & "The new location, radius " & _ 27 "and height of cylinder are" & vbCrLf & "Center = [" & _ 28 cylinder.ToString() & vbCrLf & vbCrLf 29 30 ' display CCylinder's diameter 31 output &= "Diameter is " & _ 32 String.Format("{0:F}", cylinder.Diameter()) & vbCrLf 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 34 ' display CCylinder's circumference 35 output &= "Circumference is " & _ 36 String.Format("{0:F}", cylinder.Circumference()) & vbCrLf 37 38 ' display CCylinder's area 39 output &= "Area is " & _ 40 String.Format("{0:F}", cylinder.Area()) & vbCrLf 41 42 ' display CCylinder's volume 43 output &= "Volume is " & _ 44 String.Format("{0:F}", cylinder.Volume()) 45 46 MessageBox.Show(output, "Demonstrating Class CCylinder") 47 End Sub ' Main 48 49 End Module ' modCylinderTest Class CTime inherits existing pieces of class Object. Visual Basic.NET
6.6 Constructors en Finalizers in afgeleide klasses Constructoren in afgeleide klasses Basisklasse Constructoren van de basisklasse worden niet overgeërfd door de afgeleide klasses Elke constructor van de basisklasse initialiseert de instantie-variabelen van de basisklasse die het object van de afgeleide klasse overerft Finalizers in afgeleide klasses Afgeleide klasses Uitvoering van de finalizer methode zou alle bronnen die het object heeft verworven, moeten vrijmaken voordat de garbage collector het geheugen van dat object terugvraagt Keyword MyBase wordt gebruikt om de finalizer van de basisklasse op te roepen Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.16: Point3.vb 2 ' CPoint3 class represents an x-y coordinate pair. 3 4 Public Class CPoint3 5 6 ' point coordinate 7 Private mX, mY As Integer 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to Object constructor occurs here 13 X = 0 14 Y = 0 15 Console.Writeline("CPoint3 constructor: {0}", Me) 16 End Sub ' New 17 18 ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) 21 22 ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 Console.Writeline("CPoint3 constructor: {0}", Me) 26 End Sub ' New 27 28 ' finalizer overrides version in class Object 29 Protected Overrides Sub Finalize() 30 Console.Writeline("CPoint3 Finalizer: {0}", Me) 31 MyBase.Finalize() ' call Object finalizer 32 End Sub ' Finalize 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 34 ' property X 35 Public Property X() As Integer 36 37 Get 38 Return mX 39 End Get 40 41 Set(ByVal xValue As Integer) 42 mX = xValue ' no need for validation 43 End Set 44 45 End Property ' X 46 47 ' property Y 48 Public Property Y() As Integer 49 50 Get 51 Return mY 52 End Get 53 54 Set(ByVal yValue As Integer) 55 mY = yValue ' no need for validation 56 End Set 57 58 End Property ' Y 59 60 ' return String representation of CPoint3 61 Public Overrides Function ToString() As String 62 Return "[" & mX & ", " & mY & "]" 63 End Function ' ToString 64 65 End Class ' CPoint3 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.17: Circle5.vb 2 ' CCircle5 class that inherits from class CPoint3. 3 4 Public Class CCircle5 5 Inherits CPoint3 ' CCircle5 Inherits from class CPoint3 6 7 Private mRadius As Double 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to CPoint3 constructor occurs here 13 Radius = 0 14 Console.WriteLine("CCircle5 constructor: {0}", Me) 15 End Sub ' New 16 17 ' constructor 18 Public Sub New(ByVal xValue As Integer, _ 19 ByVal yValue As Integer, ByVal radiusValue As Double) 20 21 ' use MyBase reference to CPoint3 constructor explicitly 22 MyBase.New(xValue, yValue) 23 Radius = radiusValue 24 Console.WriteLine("CCircle5 constructor: {0}", Me) 25 End Sub ' New 26 27 ' finalizer overrides version in class CPoint3 28 Protected Overrides Sub Finalize() 29 Console.Writeline("CCircle5 Finalizer: {0}", Me) 30 MyBase.Finalize() ' call CPoint3 finalizer 31 End Sub ' Finalize 32 33 ' property Radius 34 Public Property Radius() As Double 35 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 36 Get 37 Return mRadius 38 End Get 39 40 Set(ByVal radiusValue As Double) 41 42 If radiusValue > 0 43 mRadius = radiusValue 44 End If 45 46 End Set 47 48 End Property ' Radius 49 50 ' calculate CCircle5 diameter 51 Public Function Diameter() As Double 52 Return mRadius * 2 53 End Function ' Diameter 54 55 ' calculate CCircle5 circumference 56 Public Function Circumference() As Double 57 Return Math.PI * Diameter() 58 End Function ' Circumference 59 60 ' calculate CCircle5 area 61 Public Overridable Function Area() As Double 62 Return Math.PI * mRadius ^ 2 63 End Function ' Area 64 65 ' return String representation of CCircle5 66 Public Overrides Function ToString() As String 67 68 ' use MyBase reference to return CPoint3 String 69 Return "Center = " & MyBase.ToString() & _ 70 "; Radius = " & mRadius 71 End Function ' ToString 73 End Class ' CCircle5 Class CTime inherits existing pieces of class Object. Visual Basic.NET
Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.18: ConstructorAndFinalizer.vb 2 ' Display order in which base-class and derived-class constructors 3 ' and finalizers are called. 4 5 Module modConstructorAndFinalizer 6 7 Sub Main() 8 Dim circle1, circle2 As CCircle5 9 10 circle1 = New CCircle5(72, 29, 4.5) ' instantiate objects 11 circle2 = New CCircle5(5, 5, 10) 12 13 circle1 = Nothing ' mark objects for garbage collection 14 circle2 = Nothing 15 16 System.GC.Collect() ' request garbage collector to execute 17 End Sub ' Main 18 19 End Module ' modConstructorAndFinalizer Class CTime inherits existing pieces of class Object. CPoint3 constructor: Center = [72, 29]; Radius = 0 CCircle5 constructor: Center = [72, 29]; Radius = 4.5 CPoint3 constructor: Center = [5, 5]; Radius = 0 CCircle5 constructor: Center = [5, 5]; Radius = 10 CCircle5 Finalizer: Center = [5, 5]; Radius = 10 CPoint3 Finalizer: Center = [5, 5]; Radius = 10 CCircle5 Finalizer: Center = [72, 29]; Radius = 4.5 CPoint3 Finalizer: Center = [72, 29]; Radius = 4.5 Visual Basic.NET
6.7 Software engineering met overerving Overerving gebruiken om bestaande software aan te passen Creëren van nieuwe klasses Nieuwe klasses erven van oude, reeds bestaande klasses, dus op deze manier wordt aan hergebruik van software gedaan Je kunt, naargelang de nood, bijkomende members, properties en methodes toevoegen en members van de basisklasse overriden Visual Basic.NET
Alle icoontjes Visual Basic.NET