Object Oriented Design and Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oriented Design and Classes

Description:

First parameter is self the object on which the method/constructor is called ... Like functions, methods can take any number of parameters. BankAccount Class ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 11
Provided by: csUs7
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Design and Classes


1
Object Oriented Design and Classes
2
What is a class?
  • Programming mechanism to support OOD
  • Data and the methods that operate on that data
    collectively called members
  • Example bank account class
  • Provide structure for organizing programs

3
Methods
  • Methods operate on data
  • accessors read data, provide access to data but
    do not change it
  • mutators change data
  • examples from bank account, zoo
  • constructor builds a new object
  • An object in an instance of a class
  • class is a blueprint for objects

4
BankAccount Class
  • class BankAccount
  • def __init__(self, initial_balance)
  • self.balance initial_balance
  • def deposit(self, amount)
  • self.balance amount
  • def withdraw(self, amount)
  • self.balance - amount
  • def checkBalance(self)
  • print "Balance ", self.balance

5
BankAccount Class
  • def __init__(self, initial_balance)
  • self.balance initial_balance
  • __init__ method is the constructor
  • Used to initialize members of the object
  • First parameter is self the object on which the
    method/constructor is called
  • Use dot notation (.) to reference members of the
    object

6
BankAccount Class
  • def deposit(self, amount)
  • self.balance amount
  • def withdraw(self, amount)
  • self.balance - amount
  • Deposit and withdraw are mutators
  • they modify the data member balance
  • First parameter is self
  • Like functions, methods can take any number of
    parameters

7
BankAccount Class
  • def checkBalance(self)
  • print "Balance ", self.balance
  • checkBalance does not modify data members

8
Creating and Using Objects
  • b BankAccount(500)
  • //Name Type(constructor parameters)

9
Creating and Using Objects
  • b BankAccount(500)
  • //Name Type(constructor parameters)
  • //how would you withdraw funds?
  • b.withdraw(500)
  • //object_name.method_name(params)

10
Composing Objects
  • An object can have a reference to another object
  • class Name
  • def __init__(self, first, last)
  • self.first first
  • self.last last
  • def printName(self)
  • print "Name ", self.first, " ",
    self.last
  • class Employee
  • def __init__(self, first, last, pay)
  • self.name Name(first, last)
  • self.pay pay
  • def printEmployee(self)
  • self.name.printName()
  • print "\tPay rate ", self.pay
Write a Comment
User Comments (0)
About PowerShow.com