0% found this document useful (0 votes)
4 views27 pages

Starting Out With: Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Starting Out With: Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter 9

Classes and Object Oriented Programming

STARTING OUT WITH

Python
First Edition

by
Tony Gaddis

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


9.1 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.

1-2

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-2


9.1 Procedural and Object-Oriented
Programming

Two methods of programming:


• Procedural
• Object-oriented

1-3

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-3


9.1 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

1-4

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-4


9.1 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
1-5

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-5


9.1 Procedural and Object-Oriented
Programming

1-6

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-6


9.2 Classes

Concept:

A class is code that specifies data attributes and


methods for a particular type of data.

1-7

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-7


9.2 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

1-8

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-8


9.2 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

1-9

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-9


9.2 Classes

1-10

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-10


9.2 Classes
Class Definition
Program 9-1 (Coin class,
not a complete program)

1-11

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-11


9.2 Classes
Class Definition

Program 9-2
(coin_demo1.py)

1-12

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-12


9.2 Classes
Class Definition

Program 9-2 (cont.)


(coin_demo1.py)

1-13

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-13


9.2 Classes
Class Definition

Figure 9-7 The my_coin variable references a Coin object

1-14

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-14


9.2 Classes
Hiding Attributes …
__attributeName

Program 9-4
(coin_demo3.py)

1-15

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-15


9.2 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

1-16

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-16


9.2 Classes
Storing Classes in Modules
Program 9-7 (account.py) Program 9-8 (account_test.py)

1-17

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-17


9.2 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

1-18

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-18


9.3 Working with Instances

Concept:

Each instance of a class has its own set of data


attributes.

1-19

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-19


9.3 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

1-20

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-20


9.3 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.

1-21

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-21


9.4 Techniques for Designing Classes

The Unified Modeling Language (UML)


Provides a set of standard diagrams for
graphically depicting object-oriented systems

1-22

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-22


9.4 Techniques for Designing Classes
The Unified Modeling Language (UML)

Figure 9-10 General layout of a UML diagram for a class

1-23

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-23


9.4 Techniques for Designing Classes
The Unified Modeling Language (UML)
Figure 9-12 UML diagram for the CellPhone class

1-24

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-24


9.4 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.

1-25

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-25


9.4 Techniques for Designing Classes

Figure 9-13 UML diagram for the


Identifying a Class’s Customer class
Responsibility
•The things that the class is
responsible for knowing
•The actions that the class
is responsible for doing

1-26

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-26


Chapter 9
Classes and Object Oriented Programming

QUESTIONS
?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy