Lecture 25
Lecture 25
Lecture 25
Lecture Outline
– CS1001 Lecture 25 –
Polymorphism
– CS1001 Lecture 25 – 1
Dynamic binding
– CS1001 Lecture 25 – 2
Dynamic binding
– CS1001 Lecture 25 – 3
The repr method
class Person:
def __init__(self, first, last, sal):
self._firstname = first
self._lastname = last
self._salary = sal
def getInfo(self):
print(self._firstname + " " + self._lastname +
" your salary is $"+ str(self._salary))
– CS1001 Lecture 25 – 4
>>> p = Person("John", "Smith", 70000)
>>> p.getInfo()
John Smith your salary is $70000
>>> p
<person.Person object at 0x7fadfe48c470>
>>> print(p)
<person.Person object at 0x7fadfe48c470>
def __str__(self):
return self._firstname + " " + self._lastname +\
" your salary is $"+ str(self._salary)
we get:
def __repr__(self):
return self._firstname + " " + self._lastname +\
" your salary is $"+ str(self._salary + "."
– CS1001 Lecture 25 – 5
produces:
– CS1001 Lecture 25 – 6
Inheritance example
class A:
def __init__(self,i=0):
self.__i = i
def getI(self):
return self.__i
class B(A):
def __init__(self,j=0):
self.j = j
b = B()
print(b.getI())
– CS1001 Lecture 25 – 7
Inheritance example
• Consider the following code:
class Person:
def __init__(self,name):
self.__name = name
def getInfo(self):
return "Person" + self.__name
def printPerson(self):
print(self.getInfo())
class Student(Person):
def __init__(self,name,id):
super().__init__(name)
self.__id = id
def getInfo(self):
return "Student" + self.__id
– CS1001 Lecture 25 – 8
Inheritance example
– CS1001 Lecture 25 – 9
Inheritance example
# A bank account has a balance and a mechanism for making deposits
# and withdrawals
class BankAccount :
def __init__(self) :
self._balance = 0.0
def monthEnd(self) :
return
def getBalance(self) :
return self._balance
– CS1001 Lecture 25 – 10
# Overrides superclass method.
def withdraw(self, amount) :
super().withdraw(amount)
balance = self.getBalance()
if balance < self._minBalance :
self._minBalance = balance
super().withdraw(amount)
self._withdrawals = self._withdrawals + 1
if self._withdrawals > FREE_WITHDRAWALS :
super().withdraw(WITHDRAWAL_FEE)
– CS1001 Lecture 25 – 11
Inheritance example
from accounts import ChequingAccount, SavingsAccount
# Create accounts.
ACCOUNTS_SIZE = 10
accounts = []
for i in range(ACCOUNTS_SIZE // 2) :
accounts.append(ChequingAccount())
for i in range(ACCOUNTS_SIZE // 2) :
account = SavingsAccount()
account.setInterestRate(0.75)
accounts.append(account)
# Execute commands.
done = False
while not done :
action = input("D)eposit W)ithdraw M)onth end Q)uit: ")
action = action.upper()
if action == "D" or action == "W" : # Deposit or withdrawal.
num = int(input("Enter account number: "))
amount = float(input("Enter amount: "))
if action == "D" :
accounts[num].deposit(amount)
else :
accounts[num].withdraw(amount)
print("Balance:", accounts[num].getBalance())
elif action == "M" : # Month end processing.
for n in range(len(accounts)) :
– CS1001 Lecture 25 – 12
accounts[n].monthEnd()
print(n, accounts[n].getBalance())
elif action == "Q" :
done = True
– CS1001 Lecture 25 – 13