Object Oriented Programming Introduction in Python
Object Oriented Programming Introduction in Python
• There are always some rules to control how to modify the attributes
• In real life, how do you withdraw money from your account?
“Rules”
• Can you walk in the bank and get any amount even more than your
balance? Or any other bank transactions?
• Must through some “mechanism”, e.g.
• Bank tellers, ATM, phone/internet banking, etc.
• And these mechanisms have some rules,
• E.g. you cannot withdraw more than your balance
Bank Accounts with “Methods”
Attributes
Methods
Bank Accounts with “Methods”
>>> myAcc = BankAccount('Alan',1000)
>>> myAcc.showBalance()
Your balance is $1000
>>> myAcc.withdraw(123)
123
>>> myAcc.showBalance()
Your balance is $877
>>> myAcc.withdraw(99999)
Money not enough! You do not have $99999
0
Is it a *really* a new thing?
• Recall your previous lectures…
lst = [1, 2, 3]
lst.append(4)
lst [1, 2, 3, 4]
Sportscar Lorry
• Methods • Attributes
• __init__() • cargo
• turnOnTurbo() • Methods
• __init__()
• load()
• unload()
• inventory()
The Classes Vehicle and Sportcar
Sportscar Lorry
• Methods • Attributes
• __init__() • cargo
• turnOnTurbo() • Methods
• __init__()
• load()
• unload()
• inventory()
Inheritance
• Objects that exhibit similar functionality should “inherit” from the
same base object, called the superclass.
• An object that inherits from another is called the subclass.
Subclass and
Superclass
• Usually a subclass is a more
specific type of its parent
class
• Like our classification of
animals, any “children” in
the tree is a more “specific”
type of the “parents”
• (Unless we talk about
multiple inheritance later)
Let’s define a new class Bisarca
• Car carrier trailer
• It is a type of truck that carries other cars
• The truck and also load and unload, but only for cars
• not any type of cargos
Where should
we put class Vehicle
Bisarca? • Attributes: pos,velocity
• Methods:__init__(),setVelocity(),move()
Sportscar Lorry
• Methods: __init__(), • Attributes: cargo
turnOnTurbo() • Methods:__init__(),load(),unload()
,inventory()
Bisarca
• Methods: load()
Class Bisarca
Bisarca
• Methods: load()
Multiple Inheritance
Let’s Create a Class Cannon
Class Cannon
• Sample run:
>>> myCannon = Cannon()
>>> myCannon.fire()
No more ammo
>>> myCannon.reload()
Cannon reloaded
>>> myCannon.reload()
Unable to reload
>>> myCannon.fire()
Fire!!!!!!!
>>> myCannon.fire()
No more ammo
What Do You Have When You…
• Merge a cannon and a vehicle?
+ =
We Want to Have BOTH!
>>> myTank = Tank((0,0))
>>> myTank.setVelocity(40,10)
>>> myTank.move()
Move to (40, 10)
>>> myTank.move()
Move to (80, 20)
>>> myTank.reload()
Cannon reloaded
>>> myTank.fire()
Fire!!!!!!!
Where should we put the class Tank?
Vehicle Cannon
• Attributes: pos,velocity • Attributes: numAmmo
• Methods: setVelocity(),move() • Methods: fire()
Bisarca
• Methods: load()
A Bit Trouble
• Which constructor __init__() should the Tank call?
Call BOTH!!!
Resolving Methods
So far we have
Vehicle Cannon
• Attributes: pos,velocity • Attributes: numAmmo
• Methods: setVelocity(),move() • Methods: fire()
Bisarca
• Methods: load()
What Do You Have When You…
• Merge a Bisarca and a Lorry?
+ =
Let’s Construct Class BattleBisarca
Lorry
• Attributes: cargo Which
• Methods:__init__(),load() load() is
,unload(),inventory()
called by
Bisarca BattleBisarca
• Methods: load() ?
BattleBisarca
Vehicle Cannon
• Attributes: pos,velocity • Attributes: numAmmo
• Methods: setVelocity(),move() • Methods: fire()
Lorry
• Attributes: cargo The nearest
• Methods:__init__(),load() one will be
,unload(),inventory()
called
Bisarca
• Methods: load()
BattleBisarca
Let’s Constuct Class BattleBisarca
Python??