De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Visual Basic.NETPag.1 Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving Inleiding Basisklasses en Afgeleide Klasses Protected en Friend Members.

Verwante presentaties


Presentatie over: "Visual Basic.NETPag.1 Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving Inleiding Basisklasses en Afgeleide Klasses Protected en Friend Members."— Transcript van de presentatie:

1 Visual Basic.NETPag.1 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

2 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag Inleiding Enkele voorbeelden van overerving

3 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag Basisklasses en afgeleide klasses CommunityMember EmployeeStudentAlumnus FacultyStaff AdministratorTeacher Overervingshierarchie voor CCommunityMembers (universiteitsgemeenschap)

4 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag Protected en Friend members Gedeelte van een CShape klassenhierarchie CShape CTwoDimensionalShapeCThreeDimensionalShape CSphereCCubeCCylinderCTriangleCSquareCCircle

5 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag 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

6 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 6 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() ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 End Sub ' New ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 End Sub ' New ' property X 28 Public Property X() As Integer Get 31 Return mX 32 End Get 33

7 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 7 Class CTime inherits existing pieces of class Object. 34 Set(ByVal xValue As Integer) 35 mX = xValue ' no need for validation 36 End Set End Property ' X ' property Y 41 Public Property Y() As Integer Get 44 Return mY 45 End Get Set(ByVal yValue As Integer) 48 mY = yValue ' no need for validation 49 End Set End Property ' Y ' return String representation of CPoint 54 Public Overrides Function ToString() As String 55 Return "[" & mX & ", " & mY & "]" 56 End Function ' ToString End Class ' CPoint

8 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 8 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() ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New ' constructor 17 Public Sub New(ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double) ' use MyBase reference to CPoint constructor explicitly 21 MyBase.New(xValue, yValue) 22 Radius = radiusValue 23 End Sub ' New ' property Radius 26 Public Property Radius() As Double Get 29 Return mRadius 30 End Get Set(ByVal radiusValue As Double) 33

9 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 9 Class CTime inherits existing pieces of class Object. 34 If radiusValue > 0 35 mRadius = radiusValue 36 End If End Set End Property ' Radius ' calculate CCircle diameter 43 Public Function Diameter() As Double 44 Return mRadius * 2 45 End Function ' Diameter ' calculate CCircle4 circumference 48 Public Function Circumference() As Double 49 Return Math.PI * Diameter() 50 End Function ' Circumference ' calculate CCircle4 area 53 Public Overridable Function Area() As Double 54 Return Math.PI * mRadius ^ 2 55 End Function ' Area ' return String representation of CCircle4 58 Public Overrides Function ToString() As String ' use MyBase reference to return CPoint String representation 61 Return "Center= " & MyBase.ToString() & _ 62 "; Radius = " & mRadius 63 End Function ' ToString End Class ' CCircle4

10 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 10 Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.13: CircleTest4.vb 2 ' Testing class CCircle Imports System.Windows.Forms 5 6 Module modCircleTest4 7 8 Sub Main() 9 Dim circle As CCircle4 10 Dim output As String circle = New CCircle4(37, 43, 2.5) ' instantiate CCircle ' 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 ' set CCircle4's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = ' display CCircle4's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf ' display CCircle4's diameter 30 output &= "Diameter is " & _ 31 String.Format("{0:F}", circle.Diameter()) & vbCrLf 32

11 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 11 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 ' display CCircle4's area 38 output &= "Area is " & String.Format("{0:F}", circle.Area()) MessageBox.Show(output, "Demonstrating Class CCircle4") 41 End Sub ' Main End Module ' modCircleTest4

12 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag 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

13 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 13 Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.14: Cylinder.vb 2 ' CCylinder class inherits from class CCircle 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 ' 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) ' explicit call to CCircle4 constructor 20 MyBase.New(xValue, yValue, radiusValue) 21 Height = heightValue ' set CCylinder height 22 End Sub ' New ' property Height 25 Public Property Height() As Double Get 28 Return mHeight 29 End Get ' set CCylinder height if argument value is positive 32 Set(ByVal heightValue As Double) 33

14 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 14 Class CTime inherits existing pieces of class Object. 34 If heightValue >= 0 Then 35 mHeight = heightValue 36 End If End Set End Property ' Height ' 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 ' calculate CCylinder volume 48 Public Function Volume() As Double 49 Return MyBase.Area * mHeight 50 End Function ' Volume ' convert CCylinder to String 53 Public Overrides Function ToString() As String 54 Return MyBase.ToString() & "; Height = " & mHeight 55 End Function ' ToString End Class ' CCylinder

15 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 15 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 ' 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 ' properties set new x-y coordinate, radius and height 20 cylinder.X = 2 21 cylinder.Y = 2 22 cylinder.Height = cylinder.Radius = ' 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 ' display CCylinder's diameter 31 output &= "Diameter is " & _ 32 String.Format("{0:F}", cylinder.Diameter()) & vbCrLf 33

16 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 16 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 ' display CCylinder's area 39 output &= "Area is " & _ 40 String.Format("{0:F}", cylinder.Area()) & vbCrLf ' display CCylinder's volume 43 output &= "Volume is " & _ 44 String.Format("{0:F}", cylinder.Volume()) MessageBox.Show(output, "Demonstrating Class CCylinder") 47 End Sub ' Main End Module ' modCylinderTest

17 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag 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

18 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 18 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() ' implicit call to Object constructor occurs here 13 X = 0 14 Y = 0 15 Console.Writeline("CPoint3 constructor: {0}", Me) 16 End Sub ' New ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 Console.Writeline("CPoint3 constructor: {0}", Me) 26 End Sub ' New ' 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

19 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 19 Class CTime inherits existing pieces of class Object. 34 ' property X 35 Public Property X() As Integer Get 38 Return mX 39 End Get Set(ByVal xValue As Integer) 42 mX = xValue ' no need for validation 43 End Set End Property ' X ' property Y 48 Public Property Y() As Integer Get 51 Return mY 52 End Get Set(ByVal yValue As Integer) 55 mY = yValue ' no need for validation 56 End Set End Property ' Y ' return String representation of CPoint3 61 Public Overrides Function ToString() As String 62 Return "[" & mX & ", " & mY & "]" 63 End Function ' ToString End Class ' CPoint3

20 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 20 Class CTime inherits existing pieces of class Object. 1 ' Fig. 9.17: Circle5.vb 2 ' CCircle5 class that inherits from class CPoint 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() ' implicit call to CPoint3 constructor occurs here 13 Radius = 0 14 Console.WriteLine("CCircle5 constructor: {0}", Me) 15 End Sub ' New ' constructor 18 Public Sub New(ByVal xValue As Integer, _ 19 ByVal yValue As Integer, ByVal radiusValue As Double) ' 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 ' 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 ' property Radius 34 Public Property Radius() As Double 35

21 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 21 Class CTime inherits existing pieces of class Object. 36 Get 37 Return mRadius 38 End Get Set(ByVal radiusValue As Double) If radiusValue > 0 43 mRadius = radiusValue 44 End If End Set End Property ' Radius ' calculate CCircle5 diameter 51 Public Function Diameter() As Double 52 Return mRadius * 2 53 End Function ' Diameter ' calculate CCircle5 circumference 56 Public Function Circumference() As Double 57 Return Math.PI * Diameter() 58 End Function ' Circumference ' calculate CCircle5 area 61 Public Overridable Function Area() As Double 62 Return Math.PI * mRadius ^ 2 63 End Function ' Area ' return String representation of CCircle5 66 Public Overrides Function ToString() As String ' use MyBase reference to return CPoint3 String 69 Return "Center = " & MyBase.ToString() & _ 70 "; Radius = " & mRadius 71 End Function ' ToString 73 End Class ' CCircle5

22 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 22 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 CCircle circle1 = New CCircle5(72, 29, 4.5) ' instantiate objects 11 circle2 = New CCircle5(5, 5, 10) circle1 = Nothing ' mark objects for garbage collection 14 circle2 = Nothing System.GC.Collect() ' request garbage collector to execute 17 End Sub ' Main End Module ' modConstructorAndFinalizer 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

23 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag 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

24 Hoofdstuk 6: Objectgeoriënteerd programmeren: OverervingVisual Basic.NETPag. 24 Alle icoontjes


Download ppt "Visual Basic.NETPag.1 Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving Inleiding Basisklasses en Afgeleide Klasses Protected en Friend Members."

Verwante presentaties


Ads door Google