Object-oriented Programming 101 : 

Python as an example 


Tzu-li Tai (Gordon)

@ NCKU TechOrange

(facebook.com/groups/techorange.ncku)

lets build a car







What would you need?


  • 4 tires


  • 1 steering wheel


  • 1 gas pedal


  • 1 break pedal


Relax ;) Its just an example.

How do they work?


  • Turn Right:


Turn steering wheel clockwise -


          The 2 front wheels turn to the right


  • Turn Left:


Turn steering wheel anticlockwise -

          The 2 front wheels turn to the left

How do they work?


  • Go Faster:


Step down the gas pedal -


          The 2 back tires spin faster

  • Go Slower:


Step down the break pedal -
          The 2 back tires spin slower

That's basically it =)



The Attributes of a car:

The tires

A gas pedal

A break pedal

...

There's actually a lot more. We'll leave that as practice ;)

That's Basically It =)



The Methods of a Car:

Turn Left

Turn Right

Go Faster

Go Slower

Now that everything is defined. . .


We have a blueprint of what a car should be like!

We have designed a "class"



  • A Class is a blueprint of which objects are built from


  • Each object built from a class is an instance

What this looks like in Code


class Car:

    # define the attributes here...
    # and then the methods
    # This defines the class Car (the blueprint :P)

car1 = Car()    # This builds an object car1 using the class Car
car2 = Car()    # This builds an object car2, also using the class Car

So, to get this clear...

car1 and car2 are objects,

and they are instances of the class Car

As a Matter of fact...



The essence of Object-Oriented Programming

is that everything is an object


As a matter of fact...


The Steering Wheel,

the Gas/Break Pedal,

and the Tires

are objects with attributes and methods  too!

THE TIRE   



The ATTRIBUTES of a Tire

Current RPM
...
Again, we'll keep it simple for now.


The tire   



The Methods of a Tire

Rotate Faster
Rotate Slower
Pivot Right
Pivot Left

The steering wheel      



The Attributes of the Steering Wheel

The set of Tires it controls


The Steering Wheel      


The METHODS of a steering wheel

Spin Clockwise
Spin AntiClockwise

The gas / break pedal          



The ATTRIBUTES of the GAS / BREAK Pedal

The set of Tires it controls

The gas / break pedal          



The Methods of the Gas / Break Pedal

Down
Up

what have we just done?


Planned every component of a Car...

before we actually did any coding

This . . .


is the art of software designing ;)


with an OOP touch

       So   handsome
       
So   cool ;)

class Tire


 class Tire:
     
     currentRPM = 0          # a static class attribute.                             # You'll see an instance attribute in a sec

     def rotateFaster(self):
         currentRPM += 1
         print "The tire is going faster! Current RPM = " + currentRPM

     def rotateSlower(self):
         currentRPM -= 1
         print "The tire is slowing down... Current RPM = " currentRPM
def pivotRight(self): print "The tire pivots right -->" def pivotLeft(self): print "The tire pivots left <--"

CLASS STeering Wheel


 import Tire

class SteeringWheel: def __init__(self, theTires[]): self.tires[] = theTires[] # tires[] is an instance attribute def spinClockwise(self): print "Steering wheel spinning clockwise!!" self.tires[0].pivotRight() # the dot "." accesses a method self.tires[1].pivotRight() def spinAntiClockwise(self): print "Steering wheel spinning anti-clockwise!!" self.tires[0].pivotLeft() self.tires[1].pivotLeft()

class steering wheel


 import Tire

 class SteeringWheel:
 
     tires[] = {}         # use the static class attribute as default

     def __init__(self, theTires[]):
         self.tires[] = theTires[]      # replaces the default

     ...

Class Gas Pedal


 import Tire

 class GasPedal:

     def __init__(self, theTires[]):
         self.tires[] = theTires[]

     def down(self):
         print "The gas pedal goes down..."
         self.tires[2].rotateFaster()
         self.tires[3].rotateFaster()

     def up(self):
         print "The gas pedal comes up again"
         self.tires[2].rotateSlower()
         self.tires[3].rotateSlower()

Class Break Pedal


 import Tire

 class BreakPedal:

     def __init__(self, theTires[]):
         self.tires[] = theTires[]

     def down(self):
         print "The break pedal goes down..."
         self.tires[2].rotateSlower()
         self.tires[3].rotateSlower()

     def up(self):
         print "The break pedal comes up again"

Now... class car!!


 import Tire
 import SteeringWheel
 import GasPedal
 import BreakPedal

 class Car:

     def __init__(self):
         self.tires[] = [Tire()] * 4
         self.steeringWheel = SteeringWheel(tires[])
         self.gasPedal = GasPedal(tires[])
         self.breakPedal = BreakPedal(tires[])
         print "You just built a car ;)"

     ...

NOW... Class CAR


 ...
class Car: ... def goFaster(self): print "Going faster!!!" self.gasPedal.down() self.gasPedal.up() def goSlower(self): print "Slowing down..." self.breakPedal.down() self.breakPedal.up() def turnRight(self): print "Turning right!!!" self.steeringWheel.spinClockwise() def turnLeft(self): print "Turning left!!!" self.steeringWheel.spinAntiClockwise()

Lets drive =)


 import Car

 myCar = Car()

 myCar.goFaster()
 myCar.goFaster()
 myCar.goFaster()
 myCar.goSlower()
 myCar.turnRight()
 myCar.goFaster()
 myCar.turnLeft()
 myCar.goSlower()
 myCar.goSlower()
 myCar.goSlower()










HANDs-on PRACTICE



TO DO - 1



Let the class Car have an attribute numberOfTires.
Use this new attribute to rewrite
self.tires[] = [Tire()] * 4
in class Car

TO DO - 2


 
Let the Tires have the attribute tireID
so that when the Tire.goFaster() or
Tire.goSlower(), you can
print "Tire" + self.tires[0].tireID + "is rotating faster" 

Hint: Use a mix of static class attributes
and instance attributes

Do you realize

what you have just done?



You have expanded a code base

to have more functionality!

SO... 

What have we learnt

about OOP today?


  • Module/Component based design
  • Encapsulation
  • Ease of code maintenance/extension

Imagine . . .

If we are to build . . .

Truck



To be continued...
in OOP 102: Inheritance and Polymorphism
;)

Object-Oriented Programming 101: Python as an Example

By Tzu-Li Tai

Object-Oriented Programming 101: Python as an Example

Author: Tzu-Li Tai (Gordon), presented for NCKU TechOrange

  • 1,054