Title: Introduction to Computer Programming Chapter 9: Object Oriented Programming
1Introduction to Computer Programming Chapter 9
Object Oriented Programming
- Michael Scherger
- Department of Computer Science
- Kent State University
2Contents
- Creating objects of different classes in the same
program - Allow objects to communicate with each other
- Combining classes
- Inheritance
- Overriding method definitions
3Blackjack Game
- Example Blackjack Game
- Imports Cards, Games
4Sending 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()
5Sending and Receiving Messages
- Receiving a Message
- The action of executing the receiving the message
from the calling object and performing the
appropriate method
6Sending and Receiving Messages
- Example Alien Blaster Program
7Combining 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
8Combining 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
9Combining 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
10Combining Objects
- Using Card Objects
- Combining Card Objects Using a Hand Object
- Example Playing Cards Program
11Using Inheritance to Create New Classes
- Create new classes from old
- is_a relationship
- Single inheritance
- Multiple inheritance
12Extending 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
13Extending a Class Through Inheritance
- Example Playing Cards 2.0 Program
14Altering the Behavior of Inherited Methods
15Altering the Behavior of Inherited Methods
- Overriding Base Class Methods
16Altering the Behavior of Inherited Methods
- Invoking Base Class Methods
17Altering the Behavior of Inherited Methods
- Using the Derived Classes
18Altering the Behavior of Inherited Methods
- Example Playing Cards 3.0 Program
19Understanding Polymorphism
20Creating Modules
21Creating Modules
22Creating Modules
- Using Imported Functions and Classes
23Creating Modules
- Example Simple Game Program
- Imports Games
24Blackjack Game (Again)
- Example Blackjack Game
- Imports Cards, Games