Day 01 - Lesson 4 - The University of Oklahoma
Download
Report
Transcript Day 01 - Lesson 4 - The University of Oklahoma
Lesson 25
The object-oriented thought process
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Lesson 25
6/16/09
Lesson objectives
1. Define the key terms used in object-
oriented programming (OOP)
2. Understand the difference between
an object and a class
3. Describe the types of relationships
that are possible between objects
2
Python Mini-Course: Lesson 25
6/16/09
Procedural vs. OOP
Review from Lesson 6
Procedural programming separates
the program operations and the data
Object-oriented programming
packages the program operations
and the data together in object
3
Python Mini-Course: Lesson 25
6/16/09
What is an object?
The building blocks of an O-O
program
A program that uses O-O is basically
a collection of objects
Objects interact much like things in
the real world do
4
Python Mini-Course: Lesson 25
6/16/09
What is an object?
Objects have two components:
Data (i.e., attributes)
Behaviors (i.e., methods)
5
Python Mini-Course: Lesson 25
6/16/09
Object attributes
Store the data for that object
Example (taxi):
Driver
OnDuty
NumPassengers
Location
6
Python Mini-Course: Lesson 25
6/16/09
Object methods
Define the behaviors for the
object
Example (taxi):
PickUp
DropOff
GoOnDuty
•GetDriver
•SetDriver
•GetNumPassengers
GoOffDuty
7
Python Mini-Course: Lesson 25
6/16/09
Object interface
To use a method, the user
(programmer) must know:
Name of the method
Parameters to pass to the method
What (if anything) the method
returns
8
Python Mini-Course: Lesson 25
6/16/09
Object implementation
The user does NOT need to know
how the method works internally
9
Python Mini-Course: Lesson 25
6/16/09
What is a Class?
A blueprint for an object
Classes can be thought of as
templates or cookie cutters
Given a class description, we can
instantiate objects of that class
Classes are high-level data types
10
Python Mini-Course: Lesson 25
6/16/09
OOP concepts
Encapsulation
Data and behaviors are packaged
together, but the object only reveals
the interfaces needed to interact
with it
Internal data and behaviors can
remain hidden
11
Python Mini-Course: Lesson 25
6/16/09
OOP concepts
Interfaces
Fundamental means of
communication between objects
Should completely describe to user
(programmer) how to interact with
the object
Should control access to attributes
12
Python Mini-Course: Lesson 25
6/16/09
Inheritance
You can create new classes by
abstracting out common
attributes and behaviors from a
parent (or base) class
13
Python Mini-Course: Lesson 25
6/16/09
14
Python Mini-Course: Lesson 25
6/16/09
Is-a relationship
Because sub-classes inherit from
their base class, they have an isa relationship:
Lion is a cat
Cat is a mammal
15
Python Mini-Course: Lesson 25
6/16/09
Polymorphism
Allows similar objects to to
respond to the same message
(method call) in different
manners
Sub-classes can override base
class methods
16
Python Mini-Course: Lesson 25
6/16/09
OOP example: animals.py
class Animal:
def __init__(self, name):
# Constructor of the class
self.name = name
class Cat(Animal):
def talk(self):
return 'Meow!'
class Dog(Animal):
def talk(self):
return 'Woof! Woof!'
17
Python Mini-Course: Lesson 25
6/16/09
Composition
Objects can contain other objects
This is called a has-a relationship
Example:
Taxi has-a driver
18
Python Mini-Course: Lesson 25
6/16/09