240 likes | 562 Views
Lesson 25 The object-oriented thought process. Python Mini-Course University of Oklahoma Department of Psychology . Lesson objectives. Define the key terms used in object-oriented programming (OOP) Understand the difference between an object and a class
E N D
Lesson 25The object-oriented thought process Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Lesson 25
Lesson objectives • Define the key terms used in object-oriented programming (OOP) • Understand the difference between an object and a class • Describe the types of relationships that are possible between objects Python Mini-Course: Lesson 25
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 Python Mini-Course: Lesson 25
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 Python Mini-Course: Lesson 25
What is an object? • Objects have two components: • Data (i.e., attributes) • Behaviors (i.e., methods) Python Mini-Course: Lesson 25
Object attributes • Store the data for that object • Example (taxi): • Driver • OnDuty • NumPassengers • Location Python Mini-Course: Lesson 25
Object methods • Define the behaviors for the object • Example (taxi): • PickUp • DropOff • GoOnDuty • GoOffDuty • GetDriver • SetDriver • GetNumPassengers Python Mini-Course: Lesson 25
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 Python Mini-Course: Lesson 25
Object implementation • The user does NOT need to know how the method works internally Python Mini-Course: Lesson 25
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 Python Mini-Course: Lesson 25
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 Python Mini-Course: Lesson 25
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 Python Mini-Course: Lesson 25
Inheritance • You can create new classes by abstracting out common attributes and behaviors from a parent (or base) class Python Mini-Course: Lesson 25
Is-a relationship • Because sub-classes inherit from their base class, they have an is-a relationship: • Lion is a cat • Cat is a mammal Python Mini-Course: Lesson 25
Polymorphism • Allows similar objects to to respond to the same message (method call) in different manners • Sub-classes can override base class methods Python Mini-Course: Lesson 25
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!' Python Mini-Course: Lesson 25
Composition • Objects can contain other objects • This is called a has-a relationship • Example: • Taxi has-a driver Python Mini-Course: Lesson 25