De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving

Verwante presentaties


Presentatie over: "Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving"— Transcript van de presentatie:

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 Visual Basic.NET

2 6.1 Inleiding Enkele voorbeelden van overerving Visual Basic.NET

3 6.2 Basisklasses en afgeleide klasses
CommunityMember Employee Student Alumnus Faculty Staff Administrator Teacher Overervingshierarchie voor CCommunityMembers (universiteitsgemeenschap) Visual Basic.NET

4 6.3 Protected en Friend members
CShape CTwoDimensionalShape CThreeDimensionalShape CCircle CSquare CTriangle CSphere CCube CCylinder Gedeelte van een CShape klassenhierarchie Visual Basic.NET

5 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

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 ' implicitly Inherits Object 6 ' point coordinate Private mX, mY As Integer 9 ' default constructor Public Sub New() 12 ' implicit call to Object constructor occurs here X = 0 Y = 0 End Sub ' New 17 ' constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer) 21 ' implicit call to Object constructor occurs here X = xValue Y = yValue End Sub ' New 26 ' property X Public Property X() As Integer 29 Get Return mX End Get 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET

7 Class CTime inherits existing pieces of class Object.
Set(ByVal xValue As Integer) mX = xValue ' no need for validation End Set 37 End Property ' X 39 ' property Y Public Property Y() As Integer 42 Get Return mY End Get 46 Set(ByVal yValue As Integer) mY = yValue ' no need for validation End Set 50 End Property ' Y 52 ' return String representation of CPoint Public Overrides Function ToString() As String Return "[" & mX & ", " & mY & "]" End Function ' ToString 57 58 End Class ' CPoint Class CTime inherits existing pieces of class Object. Visual Basic.NET

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 Inherits CPoint ' CCircle4 Inherits from class CPoint 6 Private mRadius As Double 8 ' default constructor Public Sub New() 11 ' implicit call to CPoint constructor occurs here Radius = 0 End Sub ' New 15 ' constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer, ByVal radiusValue As Double) 19 ' use MyBase reference to CPoint constructor explicitly MyBase.New(xValue, yValue) Radius = radiusValue End Sub ' New 24 ' property Radius Public Property Radius() As Double 27 Get Return mRadius End Get 31 Set(ByVal radiusValue As Double) 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET

9 Class CTime inherits existing pieces of class Object.
If radiusValue > 0 mRadius = radiusValue End If 37 End Set 39 End Property ' Radius 41 ' calculate CCircle diameter Public Function Diameter() As Double Return mRadius * 2 End Function ' Diameter 46 ' calculate CCircle4 circumference Public Function Circumference() As Double Return Math.PI * Diameter() End Function ' Circumference 51 ' calculate CCircle4 area Public Overridable Function Area() As Double Return Math.PI * mRadius ^ 2 End Function ' Area 56 ' return String representation of CCircle4 Public Overrides Function ToString() As String 59 ' use MyBase reference to return CPoint String representation Return "Center= " & MyBase.ToString() & _ "; Radius = " & mRadius End Function ' ToString 64 65 End Class ' CCircle4 Class CTime inherits existing pieces of class Object. Visual Basic.NET

10 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 Sub Main() Dim circle As CCircle4 Dim output As String 11 circle = New CCircle4(37, 43, 2.5) ' instantiate CCircle4 13 ' get CCircle4's initial x-y coordinates and radius output = "X coordinate is " & circle.X & vbCrLf & _ "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ circle.Radius 18 ' set CCircle4's x-y coordinates and radius to new values circle.X = 2 circle.Y = 2 circle.Radius = 4.25 23 ' display CCircle4's String representation output &= vbCrLf & vbCrLf & _ "The new location and radius of circle are " & _ vbCrLf & circle.ToString() & vbCrLf 28 ' display CCircle4's diameter output &= "Diameter is " & _ String.Format("{0:F}", circle.Diameter()) & vbCrLf 32 Class CTime inherits existing pieces of class Object. Visual Basic.NET

11 Class CTime inherits existing pieces of class Object.
' display CCircle4's circumference output &= "Circumference is " & _ String.Format("{0:F}", circle.Circumference()) & vbCrLf 36 ' display CCircle4's area output &= "Area is " & String.Format("{0:F}", circle.Area()) 39 MessageBox.Show(output, "Demonstrating Class CCircle4") End Sub ' Main 42 43 End Module ' modCircleTest4 Class CTime inherits existing pieces of class Object. Visual Basic.NET

12 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

13 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 Inherits CCircle4 6 Protected mHeight As Double 8 ' default constructor Public Sub New() Height = 0 End Sub ' New 13 ' four-argument constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer, ByVal radiusValue As Double, _ ByVal heightValue As Double) 18 ' explicit call to CCircle4 constructor MyBase.New(xValue, yValue, radiusValue) Height = heightValue ' set CCylinder height End Sub ' New 23 ' property Height Public Property Height() As Double 26 Get Return mHeight End Get 30 ' set CCylinder height if argument value is positive Set(ByVal heightValue As Double) 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET

14 Class CTime inherits existing pieces of class Object.
If heightValue >= 0 Then mHeight = heightValue End If 37 End Set 39 End Property ' Height 41 ' override method Area to calculate CCylinder area Public Overrides Function Area() As Double Return 2 * MyBase.Area + MyBase.Circumference * mHeight End Function ' Area 46 ' calculate CCylinder volume Public Function Volume() As Double Return MyBase.Area * mHeight End Function ' Volume 51 ' convert CCylinder to String Public Overrides Function ToString() As String Return MyBase.ToString() & "; Height = " & mHeight End Function ' ToString 56 57 End Class ' CCylinder Class CTime inherits existing pieces of class Object. Visual Basic.NET

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 Sub Main() 9 ' instantiate object of class CCylinder Dim cylinder As New CCylinder(12, 23, 2.5, 5.7) Dim output As String 13 ' properties get initial x-y coordinate, radius and height output = "X coordinate is " & cylinder.X & vbCrLf & _ "Y coordinate is " & cylinder.Y & vbCrLf & "Radius is " & _ cylinder.Radius & vbCrLf & "Height is " & cylinder.Height 18 ' properties set new x-y coordinate, radius and height cylinder.X = 2 cylinder.Y = 2 cylinder.Height = 10 cylinder.Radius = 4.25 24 ' get new x-y coordinate and radius output &= vbCrLf & vbCrLf & "The new location, radius " & _ "and height of cylinder are" & vbCrLf & "Center = [" & _ cylinder.ToString() & vbCrLf & vbCrLf 29 ' display CCylinder's diameter output &= "Diameter is " & _ String.Format("{0:F}", cylinder.Diameter()) & vbCrLf 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET

16 Class CTime inherits existing pieces of class Object.
' display CCylinder's circumference output &= "Circumference is " & _ String.Format("{0:F}", cylinder.Circumference()) & vbCrLf 37 ' display CCylinder's area output &= "Area is " & _ String.Format("{0:F}", cylinder.Area()) & vbCrLf 41 ' display CCylinder's volume output &= "Volume is " & _ String.Format("{0:F}", cylinder.Volume()) 45 MessageBox.Show(output, "Demonstrating Class CCylinder") End Sub ' Main 48 49 End Module ' modCylinderTest Class CTime inherits existing pieces of class Object. Visual Basic.NET

17 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

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 ' point coordinate Private mX, mY As Integer 8 ' default constructor Public Sub New() 11 ' implicit call to Object constructor occurs here X = 0 Y = 0 Console.Writeline("CPoint3 constructor: {0}", Me) End Sub ' New 17 ' constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer) 21 ' implicit call to Object constructor occurs here X = xValue Y = yValue Console.Writeline("CPoint3 constructor: {0}", Me) End Sub ' New 27 ' finalizer overrides version in class Object Protected Overrides Sub Finalize() Console.Writeline("CPoint3 Finalizer: {0}", Me) MyBase.Finalize() ' call Object finalizer End Sub ' Finalize 33 Class CTime inherits existing pieces of class Object. Visual Basic.NET

19 Class CTime inherits existing pieces of class Object.
' property X Public Property X() As Integer 36 Get Return mX End Get 40 Set(ByVal xValue As Integer) mX = xValue ' no need for validation End Set 44 End Property ' X 46 ' property Y Public Property Y() As Integer 49 Get Return mY End Get 53 Set(ByVal yValue As Integer) mY = yValue ' no need for validation End Set 57 End Property ' Y 59 ' return String representation of CPoint3 Public Overrides Function ToString() As String Return "[" & mX & ", " & mY & "]" End Function ' ToString 64 65 End Class ' CPoint3 Class CTime inherits existing pieces of class Object. Visual Basic.NET

20 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 Inherits CPoint3 ' CCircle5 Inherits from class CPoint3 6 Private mRadius As Double 8 ' default constructor Public Sub New() 11 ' implicit call to CPoint3 constructor occurs here Radius = 0 Console.WriteLine("CCircle5 constructor: {0}", Me) End Sub ' New 16 ' constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer, ByVal radiusValue As Double) 20 ' use MyBase reference to CPoint3 constructor explicitly MyBase.New(xValue, yValue) Radius = radiusValue Console.WriteLine("CCircle5 constructor: {0}", Me) End Sub ' New 26 ' finalizer overrides version in class CPoint3 Protected Overrides Sub Finalize() Console.Writeline("CCircle5 Finalizer: {0}", Me) MyBase.Finalize() ' call CPoint3 finalizer End Sub ' Finalize 32 ' property Radius Public Property Radius() As Double 35 Class CTime inherits existing pieces of class Object. Visual Basic.NET

21 Class CTime inherits existing pieces of class Object.
Get Return mRadius End Get 39 Set(ByVal radiusValue As Double) 41 If radiusValue > 0 mRadius = radiusValue End If 45 End Set 47 End Property ' Radius 49 ' calculate CCircle5 diameter Public Function Diameter() As Double Return mRadius * 2 End Function ' Diameter 54 ' calculate CCircle5 circumference Public Function Circumference() As Double Return Math.PI * Diameter() End Function ' Circumference 59 ' calculate CCircle5 area Public Overridable Function Area() As Double Return Math.PI * mRadius ^ 2 End Function ' Area 64 ' return String representation of CCircle5 Public Overrides Function ToString() As String 67 ' use MyBase reference to return CPoint3 String Return "Center = " & MyBase.ToString() & _ "; Radius = " & mRadius End Function ' ToString 73 End Class ' CCircle5 Class CTime inherits existing pieces of class Object. Visual Basic.NET

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 Sub Main() Dim circle1, circle2 As CCircle5 9 circle1 = New CCircle5(72, 29, 4.5) ' instantiate objects circle2 = New CCircle5(5, 5, 10) 12 circle1 = Nothing ' mark objects for garbage collection circle2 = Nothing 15 System.GC.Collect() ' request garbage collector to execute 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

23 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

24 Alle icoontjes Visual Basic.NET


Download ppt "Hoofdstuk 6 – Objectgeoriënteerd Programmeren: Overerving"

Verwante presentaties


Ads door Google