Python Mini-Course University ofOklahoma Department of Psychology Lesson 25 The object-oriented thought process 6/16/09 Python Mini-Course: Lesson 25 1
2.
Lesson objectives 1. Definethe 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 6/16/09 Python Mini-Course: Lesson 25 2
3.
Procedural vs. OOP Reviewfrom Lesson 6 Procedural programming separates the program operations and the data Object-oriented programming packages the program operations and the data together in object 6/16/09 Python Mini-Course: Lesson 25 3
4.
What is anobject? 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 6/16/09 Python Mini-Course: Lesson 25 4
5.
What is anobject? Objects have two components: Data (i.e., attributes) Behaviors (i.e., methods) 6/16/09 Python Mini-Course: Lesson 25 5
6.
Object attributes Store thedata for that object Example (taxi): Driver OnDuty NumPassengers Location 6/16/09 Python Mini-Course: Lesson 25 6
Object interface To usea method, the user (programmer) must know: Name of the method Parameters to pass to the method What (if anything) the method returns 6/16/09 Python Mini-Course: Lesson 25 8
9.
Object implementation The userdoes NOT need to know how the method works internally 6/16/09 Python Mini-Course: Lesson 25 9
10.
What is aClass? 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 6/16/09 Python Mini-Course: Lesson 25 10
11.
OOP concepts Encapsulation Data andbehaviors are packaged together, but the object only reveals the interfaces needed to interact with it Internal data and behaviors can remain hidden 6/16/09 Python Mini-Course: Lesson 25 11
12.
OOP concepts Interfaces Fundamental meansof communication between objects Should completely describe to user (programmer) how to interact with the object Should control access to attributes 6/16/09 Python Mini-Course: Lesson 25 12
13.
Inheritance You can createnew classes by abstracting out common attributes and behaviors from a parent (or base) class 6/16/09 Python Mini-Course: Lesson 25 13
Is-a relationship Because sub-classesinherit from their base class, they have an is- a relationship: Lion is a cat Cat is a mammal 6/16/09 Python Mini-Course: Lesson 25 15
16.
Polymorphism Allows similar objectsto to respond to the same message (method call) in different manners Sub-classes can override base class methods 6/16/09 Python Mini-Course: Lesson 25 16
17.
OOP example: animals.py classAnimal: 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!' 6/16/09 Python Mini-Course: Lesson 25 17
18.
Composition Objects can containother objects This is called a has-a relationship Example: Taxi has-a driver 6/16/09 Python Mini-Course: Lesson 25 18