Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 9 9 Classes and Objects: A Deeper Look.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 9 9 Classes and Objects: A Deeper Look."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 9 9 Classes and Objects: A Deeper Look

2  2006 Pearson Education, Inc. All rights reserved. 2 Instead of this absurd division into sexes, they ought to class people as static and dynamic. — Evelyn Waugh Is it a world to hide virtues in? — William Shakespeare But what, to serve our private ends, Forbids the cheating of our friends? — Charles Churchill

3  2006 Pearson Education, Inc. All rights reserved. 3 Don’t be “consistent,” but be simply true. — Oliver Wendell Holmes, Jr. This above all: to thine own self be true. — William Shakespeare

4  2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  Encapsulation and data hiding.  The concepts of data abstraction and abstract data types (ADTs).  To use keyword this.  To use indexers to access members of a class.  To use static variables and methods.  To use readonly fields.  To take advantage of C# ’ s memory management features.  How to create a class library.  When to use the internal access modifier.

5  2006 Pearson Education, Inc. All rights reserved. 5 9.1 Introduction 9.2 Time Class Case Study 9.3 Controlling Access to Members 9.4 Referring to the Current Object’s Members with the this Reference 9.5 Indexers 9.6 Time Class Case Study: Overloaded Constructors 9.7 Default and Parameterless Constructors 9.8 Composition 9.9 Garbage Collection and Destructors 9.10 static Class Members 9.11 readonly Instance Variables 9.12 Software Reusability 9.13 Data Abstraction and Encapsulation 9.14 Time Class Case Study: Creating Class Libraries 9.15 internal Access 9.16 Class View and Object Browser 9.17 (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System 9.18 Wrap-Up

6  2006 Pearson Education, Inc. All rights reserved. 6 Outline Time1.cs (1 of 2)

7  2006 Pearson Education, Inc. All rights reserved. 7 Outline Time1.cs (2 of 2)

8  2006 Pearson Education, Inc. All rights reserved. 8 Software Engineering Observation 9.1 Methods and properties that modify the values of private variables should verify that the intended new values are proper. If they are not, they should place the private variables in an appropriate consistent state.

9  2006 Pearson Education, Inc. All rights reserved. 9 Outline Time1Test.cs (1 of 2)

10  2006 Pearson Education, Inc. All rights reserved. 10 Outline Time1Test.cs (2 of 2)

11  2006 Pearson Education, Inc. All rights reserved. 11 Software Engineering Observation 9.2 Classes simplify programming because the client can use only the public members exposed by the class. Such members are usually client oriented rather than implementation oriented. Clients are neither aware of, nor involved in, a class’s implementation. Clients generally care about what the class does but not how the class does it. (Clients do, of course, care that the class operates correctly and efficiently.)

12  2006 Pearson Education, Inc. All rights reserved. 12 Software Engineering Observation 9.3 Interfaces change less frequently than implementations. When an implementation changes, implementation-dependent code must change accordingly. Hiding the implementation reduces the possibility that other application parts will become dependent on class-implementation details.

13  2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 9.1 An attempt by a method that is not a member of a class to access a private member of that class is a compilation error.

14  2006 Pearson Education, Inc. All rights reserved. 14 Outline MemberAccessTest.cs

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline ThisTest.cs (1 of 2)

16  2006 Pearson Education, Inc. All rights reserved. 16 Outline ThisTest.cs (2 of 2)

17  2006 Pearson Education, Inc. All rights reserved. 17 Common Programming Error 9.2 It is often a logic error when a method contains a parameter or local variable that has the same name as an instance variable of the class. In such a case, use reference this if you wish to access the instance variable of the class—otherwise, the method parameter or local variable will be referenced.

18  2006 Pearson Education, Inc. All rights reserved. 18 Error-Prevention Tip 9.1 Avoid method parameter names or local variable names that conflict with field names. This helps prevent subtle, hard-to-locate bugs.

19  2006 Pearson Education, Inc. All rights reserved. 19 Performance Tip 9.1 C# conserves memory by maintaining only one copy of each method per class—this method is invoked by every object of the class. Each object, on the other hand, has its own copy of the class’s instance variables (i.e., non- static variables). Each method of the class implicitly uses the this reference to determine the specific object of the class to manipulate.

20  2006 Pearson Education, Inc. All rights reserved. 20 Common Programming Error 9.3 Declaring indexers as static is a syntax error.

21  2006 Pearson Education, Inc. All rights reserved. 21 Outline Box.cs (1 of 3)

22  2006 Pearson Education, Inc. All rights reserved. 22 Outline Box.cs (2 of 3)

23  2006 Pearson Education, Inc. All rights reserved. 23 Outline Box.cs (3 of 3)

24  2006 Pearson Education, Inc. All rights reserved. 24 Outline BoxTest.cs (1 of 2)

25  2006 Pearson Education, Inc. All rights reserved. 25 Outline BoxTest.cs (2 of 2)

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline Time2.cs (1 of 4)

27  2006 Pearson Education, Inc. All rights reserved. 27 Outline Time2.cs (2 of 4)

28  2006 Pearson Education, Inc. All rights reserved. 28 Outline Time2.cs (3 of 4)

29  2006 Pearson Education, Inc. All rights reserved. 29 Outline Time2.cs (4 of 4)

30  2006 Pearson Education, Inc. All rights reserved. 30 Common Programming Error 9.4 A constructor can call methods of the class. Be aware that the instance variables might not yet be in a consistent state, because the constructor is in the process of initializing the object. Using instance variables before they have been initialized properly is a logic error.

31  2006 Pearson Education, Inc. All rights reserved. 31 Software Engineering Observation 9.4 When one object of a class has a reference to another object of the same class, the first object can access all the second object’s data and methods (including those that are private ).

32  2006 Pearson Education, Inc. All rights reserved. 32 Software Engineering Observation 9.5 When implementing a method of a class, use the class’s properties to access the class’s private data. This simplifies code maintenance and reduces the likelihood of errors.

33  2006 Pearson Education, Inc. All rights reserved. 33 Outline Time2Test.cs (1 of 3)

34  2006 Pearson Education, Inc. All rights reserved. 34 Outline Time2Test.cs (2 of 3)

35  2006 Pearson Education, Inc. All rights reserved. 35 Outline Time2Test.cs (3 of 3)

36  2006 Pearson Education, Inc. All rights reserved. 36 Common Programming Error 9.5 If a class has constructors, but none of the public constructors are parameterless constructors, and an application attempts to call a parameterless constructor to initialize an object of the class, a compilation error occurs. A constructor can be called with no arguments only if the class does not have any constructors (in which case the default constructor is called) or if the class has a public parameterless constructor.

37  2006 Pearson Education, Inc. All rights reserved. 37 Common Programming Error 9.6 Only constructors can have the same name as the class. Declaring a method, property or field with the same name as the class is a compilation error.

38  2006 Pearson Education, Inc. All rights reserved. 38 Software Engineering Observation 9.6 One form of software reuse is composition, in which a class has as members references to objects of other classes.

39  2006 Pearson Education, Inc. All rights reserved. 39 Outline Date.cs (1 of 5)

40  2006 Pearson Education, Inc. All rights reserved. 40 Outline Date.cs (2 of 5)

41  2006 Pearson Education, Inc. All rights reserved. 41 Outline Date.cs (3 of 5)

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline Date.cs (4 of 5)

43  2006 Pearson Education, Inc. All rights reserved. 43 Outline Date.cs (5 of 5)

44  2006 Pearson Education, Inc. All rights reserved. 44 Outline Employee.cs

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline EmployeeTest.cs

46  2006 Pearson Education, Inc. All rights reserved. 46 Software Engineering Observation 9.7 A class that uses system resources, such as files on disk, should provide a method to eventually release the resources. Many FCL classes provide Close or Dispose methods for this purpose.

47  2006 Pearson Education, Inc. All rights reserved. 47 Software Engineering Observation 9.8 Use a static variable when all objects of a class must use the same copy of the variable.

48  2006 Pearson Education, Inc. All rights reserved. 48 Common Programming Error 9.7 It is a compilation error to access or invoke a static member by referencing it through an instance of the class, like a non- static member.

49  2006 Pearson Education, Inc. All rights reserved. 49 Software Engineering Observation 9.9 Static variables and methods exist, and can be used, even if no objects of that class have been instantiated.

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline Employee.cs (1 of 3)

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline Employee.cs (2 of 3)

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline Employee.cs (3 of 3)

53  2006 Pearson Education, Inc. All rights reserved. 53 Outline EmployeeTest.cs (1 of 3)

54  2006 Pearson Education, Inc. All rights reserved. 54 Outline EmployeeTest.cs (2 of 3)

55  2006 Pearson Education, Inc. All rights reserved. 55 Outline EmployeeTest.cs (3 of 3)

56  2006 Pearson Education, Inc. All rights reserved. 56 Common Programming Error 9.8 A compilation error occurs if a static method calls an instance (non- static ) method in the same class by using only the method name. Similarly, a compilation error occurs if a static method attempts to access an instance variable in the same class by using only the variable name.

57  2006 Pearson Education, Inc. All rights reserved. 57 Common Programming Error 9.9 Referring to the this reference in a static method is a syntax error.

58  2006 Pearson Education, Inc. All rights reserved. 58 Software Engineering Observation 9.10 Declaring an instance variable as readonly helps enforce the principle of least privilege. If an instance variable should not be modified after the object is constructed, declare it to be readonly to prevent modification.

59  2006 Pearson Education, Inc. All rights reserved. 59 Common Programming Error 9.10 Attempting to modify a readonly instance variable anywhere but its declaration or the object’s constructors is a compilation error.

60  2006 Pearson Education, Inc. All rights reserved. 60 Error-Prevention Tip 9.2 Attempts to modify a readonly instance variable are caught at compilation time rather than causing execution-time errors. It is always preferable to get bugs out at compile time, if possible, rather than allowing them to slip through to execution time (where studies have found that repairing is often many times more costly).

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline Increment.cs

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline IncrementTest.cs

63  2006 Pearson Education, Inc. All rights reserved. 63 Software Engineering Observation 9.11 If a readonly instance variable is initialized to a constant only in its declaration, it is not necessary to have a separate copy of the instance variable for every object of the class. The variable should be declared const instead. Constants declared with const are implicitly static, so there will only be one copy for the entire class.

64  2006 Pearson Education, Inc. All rights reserved. 64 Good Programming Practice 9.1 Avoid reinventing the wheel. Study the capabilities of the FCL. If the FCL contains a class that meets your application’s requirements, use that class rather than create your own.

65  2006 Pearson Education, Inc. All rights reserved. 65 Software Engineering Observation 9.12 Programmers create types through the class mechanism. New types can be designed to be as convenient to use as the simple types. This marks C# as an extensible language. Although the language is easy to extend via new types, the programmer cannot alter the base language itself.

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline Time1.cs (1 of 2)

67  2006 Pearson Education, Inc. All rights reserved. 67 Outline Time1.cs (2 of 2)

68  2006 Pearson Education, Inc. All rights reserved. 68 Fig. 9.17 | Creating a Class Library Project.

69  2006 Pearson Education, Inc. All rights reserved. 69 Fig. 9.18 | Adding a Reference.

70  2006 Pearson Education, Inc. All rights reserved. 70 Outline Time1NamespaceTest.cs (1 of 2)

71  2006 Pearson Education, Inc. All rights reserved. 71 Outline Time1NamespaceTest.cs (2 of 2)

72  2006 Pearson Education, Inc. All rights reserved. 72 Outline InternalAccessTest.cs (1 of 2)

73  2006 Pearson Education, Inc. All rights reserved. 73 Outline InternalAccessTest.cs (2 of 2)

74  2006 Pearson Education, Inc. All rights reserved. 74 Fig. 9.21 | Class View of class Time1 (Fig. 9.1) and class TimeTest (Fig. 9.2).

75  2006 Pearson Education, Inc. All rights reserved. 75 Fig. 9.22 | Object Browser for class Math.

76  2006 Pearson Education, Inc. All rights reserved. 76 Fig. 9.23 | Class diagram with visibility markers.

77  2006 Pearson Education, Inc. All rights reserved. 77 Fig. 9.24 | Class diagram with navigability arrows.

78  2006 Pearson Education, Inc. All rights reserved. 78 Outline Withdrawal.cs

79  2006 Pearson Education, Inc. All rights reserved. 79 Outline Withdrawal.cs

80  2006 Pearson Education, Inc. All rights reserved. 80 Outline Withdrawal.cs

81  2006 Pearson Education, Inc. All rights reserved. 81 Outline Account.cs

82  2006 Pearson Education, Inc. All rights reserved. 82 Software Engineering Observation 9.13 Many UML modeling tools can convert UML- based designs into C# code, considerably speeding up the implementation process. For more information on these “automatic” code generators, refer to the Web resources listed at the end of Section 3.10.

83  2006 Pearson Education, Inc. All rights reserved. 83 Outline Account.cs (1 of 2)

84  2006 Pearson Education, Inc. All rights reserved. 84 Outline Account.cs (2 of 2)


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 9 9 Classes and Objects: A Deeper Look."

Similar presentations


Ads by Google