Python Mini-Course - PowerPoint PPT Presentation

About This Presentation
Title:

Python Mini-Course

Description:

A program that uses O-O is basically a collection of objects ... OOP example: animals.py. class Animal: def __init__(self, name): # Constructor of the class ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 19
Provided by: troys9
Learn more at: https://www.ou.edu
Category:

less

Transcript and Presenter's Notes

Title: Python Mini-Course


1
Lesson 25The object-oriented thought process
  • Python Mini-Course
  • University of Oklahoma
  • Department of Psychology

2
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

3
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

4
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

5
What is an object?
  • Objects have two components
  • Data (i.e., attributes)
  • Behaviors (i.e., methods)

6
Object attributes
  • Store the data for that object
  • Example (taxi)
  • Driver
  • OnDuty
  • NumPassengers
  • Location

7
Object methods
  • Define the behaviors for the object
  • Example (taxi)
  • PickUp
  • DropOff
  • GoOnDuty
  • GoOffDuty
  • GetDriver
  • SetDriver
  • GetNumPassengers

8
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

9
Object implementation
  • The user does NOT need to know how the method
    works internally

10
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

11
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

12
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

13
Inheritance
  • You can create new classes by abstracting out
    common attributes and behaviors from a parent (or
    base) class

14
(No Transcript)
15
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

16
Polymorphism
  • Allows similar objects to to respond to the same
    message (method call) in different manners
  • Sub-classes can override base class methods

17
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!'

18
Composition
  • Objects can contain other objects
  • This is called a has-a relationship
  • Example
  • Taxi has-a driver
Write a Comment
User Comments (0)
About PowerShow.com