30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 Object-Oriented <strong>Program</strong>ming: Inheritance 369<br />

Testing and Debugging Tip 9.1<br />

When possible, avoid including Protected data in a base class. Rather, include non-<br />

Private properties and methods that access Private data, ensuring that the object<br />

maintains a consistent state. 9.1<br />

We reexamine our point-circle hierarchy example once more; this time, attempting <strong>to</strong><br />

use the best software engineering technique. We use CPoint (Fig. 9.4), which declares<br />

variables mX and mY as Private, and we show how derived class CCircle4 (Fig. 9.12)<br />

can invoke base-class methods and properties <strong>to</strong> manipulate these variables.<br />

1 ' Fig. 9.12: Circle4.vb<br />

2 ' CCircle4 class that inherits from class CPoint.<br />

3<br />

4 Public Class CCircle4<br />

5 Inherits CPoint ' CCircle4 Inherits from class CPoint<br />

6<br />

7 Private mRadius As Double<br />

8<br />

9 ' default construc<strong>to</strong>r<br />

10 Public Sub New()<br />

11<br />

12 ' implicit call <strong>to</strong> CPoint construc<strong>to</strong>r occurs here<br />

13 Radius = 0<br />

14 End Sub ' New<br />

15<br />

16 ' construc<strong>to</strong>r<br />

17 Public Sub New(ByVal xValue As Integer, _<br />

18 ByVal yValue As Integer, ByVal radiusValue As Double)<br />

19<br />

20 ' use MyBase reference <strong>to</strong> CPoint construc<strong>to</strong>r explicitly<br />

21 MyBase.New(xValue, yValue)<br />

22 Radius = radiusValue<br />

23 End Sub ' New<br />

24<br />

25 ' property Radius<br />

26 Public Property Radius() As Double<br />

27<br />

28 Get<br />

29 Return mRadius<br />

30 End Get<br />

31<br />

32 Set(ByVal radiusValue As Double)<br />

33<br />

34 If radiusValue > 0<br />

35 mRadius = radiusValue<br />

36 End If<br />

37<br />

38 End Set<br />

39<br />

40 End Property ' Radius<br />

Fig. 9.12 CCircle4 class that inherits from class CPoint, which does not provide<br />

Protected data (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!