You are on page 1of 6

MECN2012: Computing

Skills and Software


Development
Lecture 20:
Python - Object Oriented
Programming
1

Object Oriented
Programming
The Object Oriented approach to programming models the
world as classes of objects:
A square is an object in the polygon class
A dog is an object in the class mammals
Fido is an object

Each class contains information about itself (the classs


data attributes) and about things that it can do (the classs
functions/methods)
attribute
class Dog:
numLegs = 4
def bark(self, repeats):
print("woof "*repeats)
fido = Dog()
snoopy = Dog()

A method, which is
something the class can
do. Note that the first
argument is always self.
Two separate objects of the class Dog are created

fido.numLegs -= 1
# injured dog
print("total number of legs:", fido.numLegs + snoopy.numLegs)
# make him talk
fido.bark(3)

Call to the objects method.


Note that the argument self is

class Vehicle:
def getVehSpecs(self):
self.name = input("enter vehicle's name: ")
self.mass = float(input("enter " + self.name + "'s mass [kg]: "))
return

The vehicles own mass is accessed using self.mass

def getCrash(self, speedLimit):


legal = input("is " + self.name + " travelling at speed limit [y | n]: ")
if legal == "y":
self.speed = speedLimit
else:
self.speed = float(input("enter speed for " + self.name + " [m/s]: "))
return

Uses the objects own attibutes to calculate its energy.

def energy(self):
return 0.5*self.mass*self.speed**2 # E = 0.5*m*v^2
highwaySpeed = 120/3.6
car1 = Vehicle()
car2 = Vehicle()

# m/s
# create 2 Vehicles

car1.getVehSpecs()
car2.getVehSpecs()

# run method to get specs

car1.getCrash(highwaySpeed)
car2.getCrash(highwaySpeed)

# run method to get speed

totCrashEnergy = car1.energy() + car2.energy() # method returns car's energy


print("Total crash energy is: ", totCrashEnergy, " Joules")

Constructors
We can control how an object is created by
including a definition of the __init__() method.
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
return
def disp(self):
print("x = ", x.r, " + ", x.i, "j", sep = "")
x = Complex(3.0, -4.5)
x.disp()

>>>
x = 3.0 + -4.5j
>>>

class Point:
X = 0
Y = 0
# default coord is the origin
def setCoords(self, x, y):
self.X = x
Changes the internal variables of the Point
self.Y = y
return
def getCoords(self):
Returns the list of [x, y] coordinates
return [self.X, self.Y]
class Circle:
centre = Point()
onCircle = Point()

A Circle has two attributes of type point

def __init__(self, cX, cY, onX, onY):


self.centre.setCoords(cX, cY)
self.onCircle.setCoords(onX, onY)

# constructor method

We define how Circle is created


so that it is always properly
self.centre.X)**2
+
initialised

self.radius = ( (self.onCircle.X (self.onCircle.Y - self.centre.Y)**2 )


return

**0.5

Call the setCoords method


Point for this

def move(self, cX, cY):


of the centre
self.centre.setCoords(cX, cY)
self.onCircle.setCoords(cX + self.radius,Circle
cY)
return
def area(self):
return math.pi*self.radius**2

hulaHoop = Circle(-5, 5, -2, 9) # create circle, centre (-5,5) passing thru


(-2,9)
print("the hula hoop has radius = ", hulaHoop.radius)
print("the hula hoop has area = ", hulaHoop.area() )
hulaHoop.radius = 8
print("the hula hoop now has radius = ", hulaHoop.radius)
print("the hula hoop now has area = ", hulaHoop.area() )

hulaHoop.move(1, 1)
print("the point on the circle has moved to:", hulaHoop.onCircle.getCoords(

>>>
the
the
the
the
the
>>>

hula hoop has radius = 5.0


hula hoop has area = 78.53981633974483
hula hoop now has radius = 8
hula hoop now has area = 201.06192982974676
point on the circle has moved to: [9, 1]
6

You might also like