Lecture slides

Download Report

Transcript Lecture slides

CIT 590
Intro to Programming
Classes
Schedule change
• The upcoming HW (HW6) is your last major Python HW. It
will involve object oriented programming (Classes etc)
• 2 week HW
• Submit before spring break. No HW over spring break.
• Recursion and functional programming will be covered
next week
• You will receive a small HW(worth 10 pts as opposed to
the usual 20) sometime next week that needs to be
completed by Mar 6.
• Small HW to be done individually.
• Small HW due same date as the larger HWs
What is a class?
• Easiest to understand it as the creating new data types
• Lists, files, dictionaries etc do not represent the world
Wouldn’t it be nice if in the movie database assignment you
could say something like movie.get_actors
Writing a class is like creating a template
Nothing happens
until you start
using your class.
Classes
• Encapsulate functions that can be used for a common
purpose
• bankAccount.py
• Classes implicitly apply the principles of abstraction and
information hiding
• The user of the bank account class does not need to know how
balance is being maintained
• Just get a handle to the class and use the deposit and withdrawal
functions
Class definition
class BankAccount(object):
Parent class
def __init__ (self, initBalance):
self.balance = initBalance
def deposit(self, amount):
self.balance += amount
methods
Creating an instance of the class
• The class definition by itself does not create an instance
of the class
• The class definition is like the ice tray and the ‘create an
instance’ is like pouring water into the ice tray and getting
to set
• myFirstAccount = BankAccount()
• An instance of a class is called an object
Methods inside a class
• A class method looks very very much like a regular
function
• The first argument HAS to be ‘self’
• Calling methods within other methods
• We have to always refer to which object we are calling the method
on
def transfer (self, amount, toAccount):
self.withdraw(amount)
toAccount.deposit(amount)
Constructors (the method for creation)
• The __init__ is implicitly called when you say
B = BankAccount()
• First time we are seeing the __<function name>__
convention
• Python uses this when we have functions that are used
behind the scenes
Objects are references => be careful
• Very similar to the situation of two lists
• The copy module is your best friend
• import copy
• bankAccount2 = copy.copy(bankAccount1)
• Passing an object as a function parameter
• Again similar to lists, the object will be modified
• Sometimes you want the modification
• Sometimes you want to copy
How to print out an object
• __str__
When you call print <objectname> this internal function will
be called. Python will always provide some kind of default
However the default is ‘lame’
Try commenting out the __str__ in the bankAccount object
Let’s make our own class!
Accessing the data fields
• Python classes expose their data fields.
• For the bankaccount example you could always
manipulate the balance data field directly BUT this is
considered bad programming practice
• It is considered much better form to provide what is called
a getter and a setter
• In this bank account example, a getter for balance is fully justified
since that is a service you would want
• However a direct setting of bank balance = xyz seems
inappropriate.
• We have deposit and withdraw
• More on this when we get to Java
Inheritance
• You’ve already seen it without it being explicitly called as such
• Idea = Using methods defined on the parent class
• Software reuse
• By inheriting from a class you only need to define the specialized
methods
• Often you might be inheriting from a different developer’s class
• Checking account example
class CheckingAccount(bankAccount.BankAccount)
• Checking account inherits from bankaccount
• Checking account is a special type of bankaccount
• When should I use inheritance
• Apply the ‘is-a’ method
• A real number is a complex number
• A checking account is a bank account
Inbuilt functions to help explore inheritance
• Use isinstance
• issubclass
• Using the types module (import types)
• You can check for an int by doing
• Isinstance(3, types.IntType)
The is-a versus has-a concept
• A common method used to identify inheritance is to ask
the question ‘Is class B a special case of class A?’ If the
answer is yes, B extends A.
• If the answer is no, then you might want one class
contained inside the other – composition
• A Bank contains BankAccounts - composition
• A CheckingAccount is a BankAccount - inheritance