november 28, 2005icp: chapter 9: object oriented programming 1 introduction to computer programming...
TRANSCRIPT
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
1
Introduction to Computer Programming
Chapter 9: Object Oriented Programming
Michael Scherger
Department of Computer Science
Kent State University
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
2
Contents
• Creating objects of different classes in the same program
• Allow objects to communicate with each other
• Combining classes
• Inheritance
• Overriding method definitions
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
3
Blackjack Game
• Example: Blackjack Game– Imports: Cards, Games
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
4
Sending and Receiving Messages
• Sending a Message– When one object tells
another object to perform some action or function
hero = Player()invader = Alien()
• • •
def hero.blast(self, enemy):
enemy.die()
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
5
Sending and Receiving Messages
• Receiving a Message– The action of executing the receiving the
message from the calling object and performing the appropriate method
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
6
Sending and Receiving Messages
• Example: Alien Blaster Program
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
7
Combining Objects
• Objects can be “combined” to form other objects or collections of objects– A Car Class consists of individual Tires,
Engine, Seats,…individual parts that make up the whole
– A Zoo Class has a collection of other animals: Lions, Tigers, Bears, etc
– A FreeCheckingAccount is a type of CheckingAccount is a type of Account
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
8
Combining Objects
• Creating the Card Class– A playing card has a “rank” and a “suit”
• Constants• Constructor initializes a single Card• __str__ is used to print out the rank and suit as a
two character string
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
9
Combining Objects
• Creating the Hand Class– The Hand class is a collection of Card objects
• Uses a list• __str__ prints out the contents of each hand• clear() clears the hand of cards• add() adds a card to the list• give() gives a card to another hand
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
10
Combining Objects
• Using Card Objects
• Combining Card Objects Using a Hand Object
• Example: Playing Cards Program
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
11
Using Inheritance to Create New Classes
• Create new classes from old– “is_a” relationship– Single inheritance– Multiple inheritance
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
12
Extending a Class Through Inheritance
• Creating a Base Class– Same base classes as before
• Inheriting from a Base Class– A “deck” is a special type of hand
• It has 52 cards (populate method)• You can deal from a deck• You can shuffle a deck
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
13
Extending a Class Through Inheritance
• Example: Playing Cards 2.0 Program
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
14
Altering the Behavior of Inherited Methods
• Creating a Base Class
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
15
Altering the Behavior of Inherited Methods
• Overriding Base Class Methods
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
16
Altering the Behavior of Inherited Methods
• Invoking Base Class Methods
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
17
Altering the Behavior of Inherited Methods
• Using the Derived Classes
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
18
Altering the Behavior of Inherited Methods
• Example: Playing Cards 3.0 Program
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
19
Understanding Polymorphism
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
20
Creating Modules
• Writing Modules
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
21
Creating Modules
• Importing Modules
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
22
Creating Modules
• Using Imported Functions and Classes
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
23
Creating Modules
• Example: Simple Game Program– Imports: Games
November 28, 2005 ICP: Chapter 9: Object Oriented Programming
24
Blackjack Game (Again)
• Example: Blackjack Game– Imports: Cards, Games