Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.

Similar presentations


Presentation on theme: "Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes."— Presentation transcript:

1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes and Object Oriented Programming

2 1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Concept: Procedural programming is a method of writing software. It is a programming practice centered on the procedures or actions that take place in a program. Object-oriented programming is centered on objects. Objects are created from abstract data types that encapsulate data and function together.

3 1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Two methods of programming: Procedural Object-oriented

4 1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Procedural programming is made up of one or more procedures Procedures operate on data items that are separate from the procedure Data items are passed from one procedure to another Focus is on the creation of procedures that operate on the program’s data

5 1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Object-oriented programming is a self-contained unit that consists of data attributes and methods that operate on data attributes An object is a software entry that contains both data and procedures Data contained in an object is known as the object’s data attributes Data attributes are variables that reference data Procedures that an object performs are known as methods

6 1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Object-oriented programming ( OOP ) OOP separates code and data through encapsulation and data hiding Encapsulation refers to the combining of data and code into a single object Data hiding refers to an object’s ability to hide its data attributes from code that is outside the object Protects the data attributes from accidentally being corrupted Object reusability – it can be used by any program that needs its services

7 1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Concept: A class is code that specifies data attributes and methods for a particular type of data.

8 1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class – code that specifies the data attributes and methods of a particular type of object. It is the “blueprint” that objects may be created from Each object that is created from a class is called an instance of the class

9 1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Figure 9-3 A blueprint and houses built from the blueprint Figure 9-5 The housefly and mosquito objects are instances of the Insect class

10 1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition It is a set of statements that define a class’s methods and data attributes Starts with the keyword class, followed by the class name The self parameter is required in every method of the class It is a way of knowing which object’s data attributes the method is supposed to operate on Most python classes have the __init__ method Known as the initializer method because it initializes the object’s data attributes

11 1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Program 9-1 (Coin class, not a complete program)

12 1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Program 9-2 (coin_demo1.py)

13 1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Program 9-2 (cont.) (coin_demo1.py)

14 1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Figure 9-7 The my_coin variable references a Coin object

15 1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Hiding Attributes … __attributeName Program 9-4 (coin_demo3.py)

16 1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Storing Classes in Modules Create a module with the class definitions; save the file using the.py extension Import the module to any program that needs access to the class definition

17 1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Storing Classes in Modules Program 9-7 (account.py)Program 9-8 (account_test.py)

18 1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes The __str__ method Returns a string containing the object’s state Object’s state is the value of the object’s attribute at any given moment For example: def __str__(self): state_string = ‘The account balance is $%.2f.’ % self.__balance return state_string

19 1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Instances Concept: Each instance of a class has its own set of data attributes.

20 1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Instances When the self parameter is used to create an attribute, the attribute belongs to the specific object that self references – instance attribute(s) Figure 9-8 The coin1, coin2, and coin3 variables reference three Coin objects

21 1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Instances Accessor and Mutator Methods The accessor method returns a value from a class’s attribute but does not change it. The mutator method stores a value in a data attribute or changes the value of a data attribute in some other way.

22 1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes The Unified Modeling Language (UML) Provides a set of standard diagrams for graphically depicting object-oriented systems

23 1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes The Unified Modeling Language (UML) Figure 9-10 General layout of a UML diagram for a class

24 1-24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes The Unified Modeling Language (UML) Figure 9-12 UML diagram for the CellPhone class

25 1-25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes Finding the Classes in a Problem Get a written description of the problem domain. Identify all the nouns in the description. Each of these is a potential class. Refine the list to include only the classes that are relevant to the problem.

26 1-26 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes Identifying a Class’s Responsibility The things that the class is responsible for knowing The actions that the class is responsible for doing Figure 9-13 UML diagram for the Customer class

27 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 9 Classes and Object Oriented Programming


Download ppt "Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes."

Similar presentations


Ads by Google