Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 4 4 Introduction to Classes and Objects.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 4 4 Introduction to Classes and Objects."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 4 4 Introduction to Classes and Objects

2  2006 Pearson Education, Inc. All rights reserved. 2 Your public servants serve you right. — Adlai E. Stevenson Nothing can have value without being an object of utility. — Karl Marx Knowing how to answer one who speaks, To reply to one who sends a message. — Amenemope You will see something new. Two things. And I call them Thing One and Thing Two. — Dr. Theodor Seuss Geisel

3  2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  What classes, objects, methods and instance variables are.  How to declare a class and use it to create an object.  How to implement a class’s behaviors as methods.  How to implement a class’s attributes as instance variables and properties.  How to call an object’s methods to make the methods perform their tasks.  The differences between instance variables of a class and local variables of a method.  How to use a constructor to ensure that an object’s data is initialized when the object is created.  The differences between value types and reference types.

4  2006 Pearson Education, Inc. All rights reserved. 4 4.1 Introduction 4.2 Classes, Objects, Methods, Properties and Instance Variables 4.3 Declaring a Class with a Method and Instantiating an Object of a Class 4.4 Declaring a Method with a Parameter 4.5 Instance Variables and Properties 4.6 UML Class Diagram with a Property 4.7 Software Engineering with Properties and set and get Accessors 4.8 Value Types vs. Reference Types 4.9 Initializing Objects with Constructors 4.10 Floating-Point Numbers and Type decimal 4.11 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document 4.12 Wrap-Up

5  2006 Pearson Education, Inc. All rights reserved. 5 Outline GradeBook.cs

6  2006 Pearson Education, Inc. All rights reserved. 6 Outline GradeBookTest.cs

7  2006 Pearson Education, Inc. All rights reserved. 7 Fig. 4.3 | UML class diagram indicating that class GradeBook has a public DisplayMessage operation.

8  2006 Pearson Education, Inc. All rights reserved. 8 Outline GradeBook.cs

9  2006 Pearson Education, Inc. All rights reserved. 9 Outline GradeBookTest.cs

10  2006 Pearson Education, Inc. All rights reserved. 10 Software Engineering Observation 4.1 Normally, objects are created with new. One exception is a string literal that is contained in quotes, such as " hello ". String literals are references to string objects that are implicitly created by C#.

11  2006 Pearson Education, Inc. All rights reserved. 11 Common Programming Error 4.1 A compilation error occurs if the number of arguments in a method call does not match the number of parameters in the method declaration.

12  2006 Pearson Education, Inc. All rights reserved. 12 Common Programming Error 4.2 A compilation error occurs if the types of the arguments in a method call are not consistent with the types of the corresponding parameters in the method declaration.

13  2006 Pearson Education, Inc. All rights reserved. 13 Fig. 4.6 | UML class diagram indicating that class GradeBook has a public DisplayMessage operation with a courseName parameter of type string.

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

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

16  2006 Pearson Education, Inc. All rights reserved. 16 Software Engineering Observation 4.2 Precede every field and method declaration with an access modifier. As a rule of thumb, instance variables should be declared private and methods and properties should be declared public. If the access modifier is omitted before a member of a class, the member is implicitly declared private by default. (We will see that it is appropriate to declare certain methods private, if they will be accessed only by other methods of the class.)

17  2006 Pearson Education, Inc. All rights reserved. 17 Good Programming Practice 4.1 We prefer to list the fields of a class first, so that, as you read the code, you see the names and types of the variables before you see them used in the methods of the class. It is possible to list the class’s fields anywhere in the class outside its method declarations, but scattering them can make code difficult to read.

18  2006 Pearson Education, Inc. All rights reserved. 18 Good Programming Practice 4.2 Placing a blank line between method and property declarations enhances application readability.

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

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

21  2006 Pearson Education, Inc. All rights reserved. 21 Fig. 4.9 | UML class diagram indicating that class GradeBook has a public CourseName property of type string and one public method.

22  2006 Pearson Education, Inc. All rights reserved. 22 Software Engineering Observation 4.3 Accessing private data through set and get accessors not only protects the instance variables from receiving invalid values, but also hides the internal representation of the instance variables from that class’s clients. Thus, if representation of the data changes (often to reduce the amount of required storage or to improve performance), only the properties’ implementations need to change—the clients’ implementations need not change as long as the services provided by the properties are preserved.

23  2006 Pearson Education, Inc. All rights reserved. 23 Fig. 4.10 | Value type variable.

24  2006 Pearson Education, Inc. All rights reserved. 24 Software Engineering Observation 4.4 A variable’s declared type (e.g., int, double or GradeBook ) indicates whether the variable is of a value or a reference type. If a variable’s type is not one of the thirteen simple types, or an enum or a struct type (which we discuss in Section 7.10 and Chapter 16, respectively), then it is a reference type. For example, Account account1 indicates that account1 is a variable that can refer to an Account object.

25  2006 Pearson Education, Inc. All rights reserved. 25 Fig. 4.11 | Reference type variable.

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

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

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

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

30  2006 Pearson Education, Inc. All rights reserved. 30 Error-Prevention Tip 4.1 Unless default initialization of your class’s instance variables is acceptable, provide a constructor to ensure that your class’s instance variables are properly initialized with meaningful values when each new object of your class is created.

31  2006 Pearson Education, Inc. All rights reserved. 31 Fig. 4.14 | UML class diagram indicating that class GradeBook has a constructor with a name parameter of type string.

32  2006 Pearson Education, Inc. All rights reserved. 32 Common Programming Error 4.3 Using floating-point numbers in a manner that assumes they are represented precisely can lead to logic errors.

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

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

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

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

37  2006 Pearson Education, Inc. All rights reserved. 37 Fig. 4.17 | string format specifiers.

38  2006 Pearson Education, Inc. All rights reserved. 38 Error-Prevention Tip 4.2 The benefits of data integrity are not automatic simply because instance variables are made private —you must provide appropriate validity checking and report the errors.

39  2006 Pearson Education, Inc. All rights reserved. 39 Error-Prevention Tip 4.3 set accessors that set the values of private data should verify that the intended new values are proper; if they are not, the set accessors should leave the instance variables unchanged and generate an error. We demonstrate how to gracefully generate errors in Chapter 12, Exception Handling.

40  2006 Pearson Education, Inc. All rights reserved. 40 Fig. 4.18 | UML class diagram indicating that class Account has a public Balance property of type decimal, a constructor and a method.

41  2006 Pearson Education, Inc. All rights reserved. 41 Fig. 4.19 | Nouns and noun phrases in the requirements document.

42  2006 Pearson Education, Inc. All rights reserved. 42 Fig. 4.20 | Representing a class in the UML using a class diagram.

43  2006 Pearson Education, Inc. All rights reserved. 43 Fig. 4.21 | Class diagram showing an association among classes.

44  2006 Pearson Education, Inc. All rights reserved. 44 Fig. 4.22 | Multiplicity types.

45  2006 Pearson Education, Inc. All rights reserved. 45 Fig. 4.23 | Class diagram showing composition relationships.

46  2006 Pearson Education, Inc. All rights reserved. 46 Fig. 4.24 | Class diagram for the ATM system model.

47  2006 Pearson Education, Inc. All rights reserved. 47 Fig. 4.25 | Class diagram showing some composition relationships of a class Car.

48  2006 Pearson Education, Inc. All rights reserved. 48 Fig. 4.26 | Class diagram for the ATM system model including class Deposit.


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 4 4 Introduction to Classes and Objects."

Similar presentations


Ads by Google