Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

3  2007 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  What classes, objects, methods, instance variables and properties 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 them 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 attributes are initialized when the object is created.  The differences between value types and reference types.  How to use properties to ensure that only valid data is placed in attributes.

4  2007 Pearson Education, Inc. All rights reserved. 4 4.1Introduction 4.2Classes, Objects, Methods and Instance Variables 4.3Declaring a Class with a Method and Instantiating an Object of a Class 4.4Declaring a Method with a Parameter 4.5Instance Variables and Properties 4.6Value Types and Reference Types 4.7Initializing Objects with Constructors 4.8Validating Data with Set Accessors in Properties 4.9(Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document 4.10Wrap-Up

5  2007 Pearson Education, Inc. All rights reserved. 5 4.1 Introduction Classes – Properties – Methods – Constructors

6  2007 Pearson Education, Inc. All rights reserved. 6 4.2 Classes, Objects, Methods, Properties and Instance Variables Class provides one or more methods Method represents task in a program – Describes the mechanisms that actually perform its tasks – Hides from its user the complex tasks that it performs – Method call tells method to perform its task Classes contain one or more attributes – Specified by instance variables – Carried with the object as it is used

7  2007 Pearson Education, Inc. All rights reserved. 7 4.3 Declaring a Class with a Method and Instantiating an Object of a Class Class GradeBook – Keyword Public is an access modifier – Class declarations include: Access modifier Keyword Class The corresponding End Class Statement

8  2007 Pearson Education, Inc. All rights reserved. 8 4.3 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Class GradeBook – Method declarations Keyword Public indicates method is available to the public Keyword Sub indicates that there is no return type Access modifier, name of method, parentheses and return type, comprise the method header Naming convention is to capitalize the first letter of every word

9  2007 Pearson Education, Inc. All rights reserved. 9 Outline GradeBook.vb The method header for DisplayMessage() When method is called, it outputs to screen

10  2007 Pearson Education, Inc. All rights reserved. 10 4.3 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Module GradeBookTest – Any class or module that contains a Main method can be used to execute an application – Visual Basic is extensible Programmers can create new classes – Class instance creation expression Keyword New Then name of class to create and parentheses – Calling a method Object’s name, then dot separator (. ) Then method’s name and parentheses

11  2007 Pearson Education, Inc. All rights reserved. 11 Outline GradeBookTest.vb Use class instance creation expression to create object of class GradeBook Call method DisplayMessage using GradeBook object

12  2007 Pearson Education, Inc. All rights reserved. 12 4.3 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) UML class diagrams (Fig. 4.3) – Top compartment contains name of the class – Middle compartment contains class’s attributes or instance variables – Bottom compartment contains class’s operations or methods Plus sign indicates Public modifier

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

14  2007 Pearson Education, Inc. All rights reserved. 14 4.4 Declaring a Method with a Parameter Method parameters (Fig. 4.4) – Additional information passed to a method – Supplied in the method call with arguments Input method (Fig. 4.5) – Console.ReadLine reads a line of input

15  2007 Pearson Education, Inc. All rights reserved. 15 Outline GradeBook.vb Method header DisplayMessage that takes a courseName argument of type String Output message with the argument when method is called

16  2007 Pearson Education, Inc. All rights reserved. 16 Outline GradeBookTest.vb Call ReadLine method to read a line of input and assigns it to nameOfCourse Call DisplayMessage with an argument

17  2007 Pearson Education, Inc. All rights reserved. 17 4.4 Declaring a Method with a Parameter (Cont.) Parameters specified in method’s parameter list – Part of method header – Uses a comma-separated list – Keyword ByVal The argument is passed by value UML class diagram (Fig. 4.6) – Parameters specified by parameter name followed by a colon and parameter type

18  2007 Pearson Education, Inc. All rights reserved. 18 Fig. 4.6 | UML class diagram indicating that class GradeBook has a DisplayMessage operation with a courseName parameter of type String.

19  2007 Pearson Education, Inc. All rights reserved. 19 4.5 Instance Variables and Properties Variables declared in the body of method – Called local variables – Can only be used within that method Variables declared in a class declaration – Called fields or instance variables – Each object of the class has a separate instance of the variable

20  2007 Pearson Education, Inc. All rights reserved. 20 Outline GradeBook.vb Instance variable courseNameValue Get accessor for property courseNameValue Set accessor for property courseNameValue Calls the Get accessor of property CourseName

21  2007 Pearson Education, Inc. All rights reserved. 21 4.5 Instance Variables and Properties (Cont.) Predefined constant identifiers – vbCrLf Represents a combination of carriage return and linefeed character Outputting this constant’s value causes subsequent text to display at the beginning of the next line – vbTab A constant that represents a Tab character

22  2007 Pearson Education, Inc. All rights reserved. 22 4.5 Instance Variables and Properties (Cont.) Private keyword – Used for most instance variables – Private variables and methods are accessible only to methods of the class in which they are declared – Declaring instance variables Private is known as information hiding

23  2007 Pearson Education, Inc. All rights reserved. 23 Software Engineering Observation 4.1 Precede every instance variable declaration, method declaration and property declaration with an access modifier. In most cases, instance variables should be declared Private, and methods and properties should be declared Public. If these modifiers are omitted, instance variables are Private by default, and methods and properties are Public by default. (We will see in Section 9.2 that it is appropriate to declare certain methods and properties Private if they should be accessed only by other methods and properties of the class.)

24  2007 Pearson Education, Inc. All rights reserved. 24 Software Engineering Observation 4.2 Declaring the instance variables of a class as Private and the methods of the class as Public facilitates debugging, because problems with data manipulations are localized to the class’s methods and properties.

25  2007 Pearson Education, Inc. All rights reserved. 25 4.5 Instance Variables and Properties (Cont.) Property Declaration – Declaration consist of an access modifier, keyword Property, name with parentheses, and type – Get and Set allows you to access and modify private variables outside of the class, respectively – Contain a Get accessor, Set accessor, or both – After defining a property, you can use it like a variable ( object_Name.Property_Name ) Get and Set Accessors – Get accessor contains a Return statement – Set accessor assigns a value to its corresponding instance variable

26  2007 Pearson Education, Inc. All rights reserved. 26 4.5 Instance Variables and Properties (Cont.) Default initial value – Provided for all fields not initialized 0 for numeric/value type variables Nothing for String s and reference types

27  2007 Pearson Education, Inc. All rights reserved. 27 Outline GradeBookTest.vb Calls the Get accessor of property CourseName Calls the Set accessor of property CourseName Calls the Get accessor of property CourseName

28  2007 Pearson Education, Inc. All rights reserved. 28 4.5 Instance Variables and Properties (Cont.) UML Diagram – Model properties in the UML as attributes: Public is indicated by the “+” sign > Property’s name “:” property’s type If the property only contains a Get accessor, then place “{ReadOnly}” after the property’s type – Modeling Private instance variables that are not properties: Attribute’s name “:” attribute’s type Private is indicated by the “-” sign

29  2007 Pearson Education, Inc. All rights reserved. 29 Fig. 4.9 | UML class diagram indicating that class GradeBook has a courseNameValue attribute of type String, one property and one method.

30  2007 Pearson Education, Inc. All rights reserved. 30 4.5 Instance Variables and Properties (Cont.) Public variable can be read or written by any property or method Private variables can only be access indirectly through the class’s non- Private properties – Class able to control how the data is set or returned – Allows for data validation Properties of a class should use class’s own methods to manipulate the class’s Private instance variables – Creates more robust class

31  2007 Pearson Education, Inc. All rights reserved. 31 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.

32  2007 Pearson Education, Inc. All rights reserved. 32 4.6 Value Types vs. Reference Types Types in Visual Basic – Value (primitive types except String ) Contains a value of that type List of Primitive Types in Appendix L – Reference (sometimes called non-primitive types) Objects Default value of Nothing Used to invoke an object’s methods and properties

33  2007 Pearson Education, Inc. All rights reserved. 33 Fig. 4.10 | Value type variable.

34  2007 Pearson Education, Inc. All rights reserved. 34 Fig. 4.11 | Reference type variable.

35  2007 Pearson Education, Inc. All rights reserved. 35 4.7 Initializing Objects with Constructors Constructors – Initialize an object of a class – Visual Basic requires a constructor for every class – Visual Basic will provide a default no-argument constructor ONLY when none is provided – Called when keyword New is followed by the class name and parentheses – Constructors can also take arguments – Constructor header similar to regular Sub method header except the name is replaced with keyword New

36  2007 Pearson Education, Inc. All rights reserved. 36 Outline GradeBook.vb Constructor to initialize courseNameValue variable

37  2007 Pearson Education, Inc. All rights reserved. 37 Outline GradeBookTest.vb Call constructor to create first grade book object Create second grade book object

38  2007 Pearson Education, Inc. All rights reserved. 38 4.9 Initializing Objects with Constructors (Cont.) UML class diagram – Constructors go in third compartment – Place “ >” before New and its arguments – By convention, place constructors first in their compartment

39  2007 Pearson Education, Inc. All rights reserved. 39 Fig. 4.14 | UML class diagram indicating that class GradeBook has a constructor that has a name parameter of type String.

40  2007 Pearson Education, Inc. All rights reserved. 40 4.8 Validating Data with Set Accessors in Properties Validations should be made in the Set accessor to check if the data is valid By default, the Get and Set accessor has the same access as the property, however they can vary. String – Length property returns the number of characters in the String – Substring returns a new String object created by copying part of an existing String object – To display a double quote, use two double quotes in a row

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

42  2007 Pearson Education, Inc. All rights reserved. 42 Outline GradeBook.vb (1 of 2 ) Uses the validation provided in the Set accessor

43  2007 Pearson Education, Inc. All rights reserved. 43 Outline GradeBook.vb (2 of 2 ) Validation included value is valid! value is invalid: take substring!

44  2007 Pearson Education, Inc. All rights reserved. 44 Outline GradeBookTest.vb (1 of 2 ) Argument is too longArgument is valid

45  2007 Pearson Education, Inc. All rights reserved. 45 Outline GradeBookTest.vb (2 of 2 )

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

47  2007 Pearson Education, Inc. All rights reserved. 47 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 place the Private instance variables into an appropriately consistent state.

48  2007 Pearson Education, Inc. All rights reserved. 48 4.9 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document Begin designing the ATM system – Analyze the nouns and noun phrases – Introduce UML class diagrams

49  2007 Pearson Education, Inc. All rights reserved. 49 4.9 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) Key nouns and noun phrases in requirements document – Some are attributes of other classes – Some do not correspond to parts of the system – Some are classes To be represented by UML class diagrams

50  2007 Pearson Education, Inc. All rights reserved. 50 Fig. 4.17 | Nouns and noun phrases in the requirements document.

51  2007 Pearson Education, Inc. All rights reserved. 51 4.9 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) UML class diagrams – Top compartment contains name of the class – Middle compartment contains class’s attributes or instance variables – Bottom compartment contains class’s operations or methods

52  2007 Pearson Education, Inc. All rights reserved. 52 Fig. 4.18 | Representing a class in the UML using a class diagram.

53  2007 Pearson Education, Inc. All rights reserved. 53 Fig. 4.19 | Class diagram showing an association among classes.

54  2007 Pearson Education, Inc. All rights reserved. 54 4.9 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) UML class diagrams – Allows suppression of class attributes and operations Called an elided diagram – Solid line that connects two classes represents an association numbers near end of each line are multiplicity values

55  2007 Pearson Education, Inc. All rights reserved. 55 Fig. 4.20 | Multiplicity types.

56  2007 Pearson Education, Inc. All rights reserved. 56 4.9 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) UML class diagrams – Solid diamonds attached to association lines indicate a composition relationship – Hollow diamonds indicate aggregation – a weaker form of composition

57  2007 Pearson Education, Inc. All rights reserved. 57 Fig. 4.21 | Class diagram showing composition relationships.

58  2007 Pearson Education, Inc. All rights reserved. 58 Fig. 4.22 | Class diagram for the ATM system model.

59  2007 Pearson Education, Inc. All rights reserved. 59 Fig. 4.23 | Class diagram showing some composition relationships of a class Car.

60  2007 Pearson Education, Inc. All rights reserved. 60 Fig. 4.24 | Class diagram for the ATM system model including class Deposit.


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

Similar presentations


Ads by Google