Object-Oriented Programming: Computer Science & Engineering Department The Chinese University of Hong Kong
Object-Oriented Programming: Computer Science & Engineering Department The Chinese University of Hong Kong
Programming
Computer Science & Engineering Department
The Chinese University of Hong Kong
1. Code Complete: A Practical Handbook of Software Construction, Second Edition 2nd Edition
Steve McConnelMicrosoft Press; 2nd edition (June 19, 2004)
CSCI2040 INTRODUCTION TO PYTHON 4
Object-Oriented Programming (OOP)
• One of the most popular programming paradigms
• OOP emphasize the concept of software reuse -
once a certain program is being written, later others can build a new
prototype base on current one with minimal modification
Behavior/Method declarations
CSCI2040 INTRODUCTION TO PYTHON 9
1 # Rocket is a class which simulates a rocket ship
2 class Rocket():
3 def __init__(self): # Each rocket has an (x,y) position, and init to 0
4 self.x = 0
5 self.y = 0
6
7 def move_up(self): # a method which moves the rocket ship up by 1 unit
8 self.y = self.y + 1
9
10 # One can *instantiate* an instance of the Rocket class
11 my_rocket = Rocket() # instantiate an instance
12 # my_rocket has a copy of each of the class's variables,
13 # and it can do any action that is defined for the class.
14 print(my_rocket) # shows that my_rocket is stored at a particular location
15 print('my_rocket x is = ', my_rocket.x, ", my rocket y is = ", my_rocket.y)
Attributes declarations
CSCI2040 INTRODUCTION TO PYTHON 10
1 # Let's see how we can invoke the class's method
2
3 my_rocket = Rocket() # instantiate an instance
4 for counter in range(3):
5 my_rocket.move_up() # invoke class function
6
7 print('my_rocket x is = ', my_rocket.x, ", my rocket y is = ", my_rocket.y)
8
9
10
11
12
13
14
15
my_rocket x is = 0 , my rocket y is = 3
CSCI2040 INTRODUCTION TO PYTHON 11
1 # The following code shows that one can create **multiple instances** of
2 # Create a fleet of 5 rockets, and store them in a list.
3
4
5 my_rockets = [ ]
6 for x in range(0,5): # go through the loop 5 times
7 new_rocket = Rocket()
8 my_rockets.append(new_rocket) # my_rocket contains 5 Rocket instances
9
10
11 # Show that each rocket is a separate object.
12 for rocket in my_rockets:
13 print(rocket)
14
15
calc = CalculatorEngine()
calc.pushOperand( 3 )
calc.pushOperand( 4 )
calc.doTextOp ( '*' )
print calc.currentOperand()
• Advantages:
1.Make program easier to understand
2.Enable software reuse e.g. stack
3.Division of calculator engine and interface
• View is the object that interacts with end user
• Model is the logic that actually implements the tasks being performed
• Can also change the interface to others without need to rewritten the calculator
engine CSCI2040 INTRODUCTION TO PYTHON 30
Types & Tests
• Each class definition creates a new type
>>> print (type(myAccount))
<class '__main__.BankAccount'>
>>> print (type(BankAccount))
<type 'type'>
class A(object):
def doa(self):
print ("I'm a“)
class B (object):
def dob(self):
print ("I'm b“)
class C(A,B):
def doc(self):
print ("I'm c“)
>>> v=C()
>>> v.doc()
I'm c
>>> v.doa()
I'm a
>>> v.dob()
I'm b
class CountingClass:
count = 0
def __init__(self):
CountingClass.count = CountingClass.count + 1
>>>a = CountingClass()
>>>b = CountingClass()
>>>print (CountingClass.count)
2
class Box(object):
def __init__( self, v):
self.value = v
def newScope(x):
y = x
y.value = 42
a = Box(3)
newScope(a)
print (a.value)
>>>42 CSCI2040 INTRODUCTION TO PYTHON 36
Qualified Names
• A period following a base e.g. object.attribute
• Base is first determined using LEGB rule
• Names can be qualified include
• Classes
• Instances or objects
• Instances of built-in types e.g. list, dictionary
• Modules
>>> CountingClass.__dict__
{'count': 2, '__module__': '__main__', '__doc__':
None, '__init__': <function __init__ at
0x00FDE4F0>}
if __name__ == '__main__':
.. statements
CSCI2040 INTRODUCTION TO PYTHON 43