@ NCKU TechOrange
(facebook.com/groups/techorange.ncku)

Relax ;) Its just an example.


Turn steering wheel clockwise -
The 2 front wheels turn to the right
Turn steering wheel anticlockwise -
The 2 front wheels turn to the left
Step down the gas pedal -
The 2 back tires spin faster
Step down the break pedal -
The 2 back tires spin slower
The tires
A gas pedal
A break pedal
...
There's actually a lot more. We'll leave that as practice ;)
Turn Left
Turn Right
Go Faster
Go Slower

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
The essence of Object-Oriented Programming
is that everything is an object
The Steering Wheel,
the Gas/Break Pedal,
and the Tires







is the art of software designing ;)
with an OOP touch
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 = " currentRPMdef pivotRight(self): print "The tire pivots right -->" def pivotLeft(self): print "The tire pivots left <--"
import Tireclass 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()
import Tire
class SteeringWheel:
tires[] = {} # use the static class attribute as default
def __init__(self, theTires[]):
self.tires[] = theTires[] # replaces the default
...
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()
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"
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 ;)"
...
...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()
import Car
myCar = Car()
myCar.goFaster()
myCar.goFaster()
myCar.goFaster()
myCar.goSlower()
myCar.turnRight()
myCar.goFaster()
myCar.turnLeft()
myCar.goSlower()
myCar.goSlower()
myCar.goSlower()