Announcements Lecture ¡15: • Homework ¡5 ¡due ¡Wednesday ¡3/4 ¡@ ¡11:59pm ¡ Inheritance • Project ¡3 ¡due ¡Thursday ¡3/12 ¡@ ¡11:59pm ¡ • Midterm ¡2 ¡on ¡Thursday ¡3/19 ¡7pm-‑9pm ¡ 2/27/2015 ¡ • Quiz ¡2 ¡released ¡Wednesday ¡3/4 ¡ Guest ¡Lecturer: ¡Marvin ¡Zhang • Due ¡Thursday ¡3/5 ¡@ ¡11:59pm ¡ • Object-‑oriented ¡programming ¡ • Similar ¡to ¡homework ¡5 ¡ • Guerrilla ¡section ¡this ¡Sunday ¡3/1 ¡on ¡mutation Some ¡(a ¡lot ¡of) ¡material ¡from ¡these ¡slides ¡was ¡borrowed ¡from ¡John ¡DeNero. Inheritance Inheritance ¡Example • Powerful ¡idea ¡in ¡Object-‑Oriented ¡Programming ¡ class Account: class CheckingAccount(Account): """A checking account.""" """A bank account.""" ... ... • Way ¡of ¡ relating ¡similar ¡classes ¡together ¡ • Checking ¡accounts ¡have: ¡ • Bank ¡accounts ¡have: ¡ • Common ¡use: ¡a ¡ specialized ¡class ¡inherits ¡from ¡a ¡more ¡ • an ¡account ¡holder ¡ general ¡class ¡ • an ¡account ¡holder ¡ • a ¡balance ¡ class <new class>(<base class>): • a ¡balance ¡ • an ¡interest ¡rate ¡of ¡1% ¡ ... • an ¡interest ¡rate ¡of ¡2% ¡ • a ¡withdraw ¡fee ¡of ¡$1 ¡ • The ¡new ¡class ¡ shares ¡attributes ¡with ¡the ¡base ¡class, ¡ • You ¡can: ¡ • You ¡can: ¡ and ¡ overrides ¡certain ¡attributes ¡ • deposit ¡to ¡a ¡checking ¡account ¡ • deposit ¡to ¡an ¡account ¡ • Implementing ¡the ¡new ¡class ¡is ¡now ¡as ¡simple ¡as ¡ • withdraw ¡from ¡a ¡checking ¡account ¡ specifying ¡how ¡it’s ¡ different ¡from ¡the ¡base ¡class • withdraw ¡from ¡an ¡account (but ¡there’s ¡a ¡fee!) Inheritance ¡Example Inheritance ¡Example class CheckingAccount(Account): class CheckingAccount(Account): class Account: class Account: """A checking account.""" """A checking account.""" """A bank account.""" """A bank account.""" ... ... ... ... • Checking ¡accounts ¡have: ¡ • Checking ¡accounts ¡have: ¡ • Bank ¡accounts ¡have: ¡ • Bank ¡accounts ¡have: ¡ • an ¡account ¡holder ¡ • an ¡account ¡holder ¡ • an ¡account ¡holder ¡ • an ¡account ¡holder ¡ • a ¡balance ¡ • a ¡balance ¡ • a ¡balance ¡ • a ¡balance ¡ • an ¡interest ¡rate ¡of ¡1% ¡ • an ¡interest ¡rate ¡of ¡1% ¡ • an ¡interest ¡rate ¡of ¡2% ¡ • an ¡interest ¡rate ¡of ¡2% ¡ • a ¡withdraw ¡fee ¡of ¡$1 ¡ • a ¡withdraw ¡fee ¡of ¡$1 ¡ • You ¡can: ¡ • You ¡can: ¡ • You ¡can: ¡ • You ¡can: ¡ • deposit ¡to ¡a ¡checking ¡account ¡ • deposit ¡to ¡a ¡checking ¡account ¡ • deposit ¡to ¡an ¡account ¡ • deposit ¡to ¡an ¡account ¡ • withdraw ¡from ¡a ¡checking ¡account ¡ • withdraw ¡from ¡a ¡checking ¡account ¡ • withdraw ¡from ¡an ¡account • withdraw ¡from ¡an ¡account (but ¡there’s ¡a ¡fee!) (but ¡there’s ¡a ¡fee!) Inheritance ¡Example Attribute ¡Look ¡Up (demo) class CheckingAccount(Account): class Account: To ¡look ¡up ¡a ¡name ¡in ¡a ¡class: ¡ """A checking account.""" """A bank account.""" ... ... 1. If ¡the ¡name ¡is ¡in ¡the ¡attributes ¡of ¡the ¡class, ¡return ¡the ¡ corresponding ¡value ¡ • Checking ¡accounts ¡have: ¡ • Bank ¡accounts ¡have: ¡ • an ¡account ¡holder ¡ 2. If ¡not ¡found, ¡look ¡up ¡the ¡name ¡in ¡the ¡base ¡class, • an ¡account ¡holder ¡ if ¡there ¡is ¡one ¡ • a ¡balance ¡ • a ¡balance ¡ Base ¡class ¡attributes ¡ are ¡not ¡copied ¡into ¡subclasses! ¡ • an ¡interest ¡rate ¡of ¡1% ¡ • an ¡interest ¡rate ¡of ¡2% ¡ >>> tom = CheckingAccount('Tom') # Account.__init__ • a ¡withdraw ¡fee ¡of ¡$1 ¡ >>> tom.interest # Found in CheckingAccount • You ¡can: ¡ • You ¡can: ¡ 0.01 >>> tom.deposit(20) # Found in Account • deposit ¡to ¡a ¡checking ¡account ¡ • deposit ¡to ¡an ¡account ¡ 20 >>> tom.withdraw(5) # Found in CheckingAccount • withdraw ¡from ¡a ¡checking ¡account ¡ • withdraw ¡from ¡an ¡account 14 (but ¡there’s ¡a ¡fee!)
Recommend
More recommend