6
MECN2012: Computing Skills and Software Development Lecture 20: Python - Object Oriented Programming 1

Lecture+20+-+Object+Oriented+Programming

  • Upload
    ali-gh

  • View
    215

  • Download
    0

Embed Size (px)

DESCRIPTION

j

Citation preview

Page 1: Lecture+20+-+Object+Oriented+Programming

1

MECN2012: Computing Skills and Software Development

Lecture 20:Python - Object Oriented

Programming

Page 2: Lecture+20+-+Object+Oriented+Programming

2

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 class’s data attributes) and about things that it can do (the class’s functions/methods)class Dog: numLegs = 4 def bark(self, repeats): print("woof "*repeats)

fido = Dog()snoopy = Dog()

fido.numLegs -= 1 # injured dogprint("total number of legs:", fido.numLegs + snoopy.numLegs)

# make him talkfido.bark(3)

attribute

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

Call to the object’s method. Note that the argument self is not passed

Page 3: Lecture+20+-+Object+Oriented+Programming

3

class Vehicle: def getVehSpecs(self): self.name = input("enter vehicle's name: ") self.mass = float(input("enter " + self.name + "'s mass [kg]: ")) return 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 def energy(self): return 0.5*self.mass*self.speed**2 # E = 0.5*m*v^2

highwaySpeed = 120/3.6 # m/scar1 = Vehicle() # create 2 Vehiclescar2 = Vehicle()

car1.getVehSpecs() # run method to get specscar2.getVehSpecs()

car1.getCrash(highwaySpeed) # run method to get speedcar2.getCrash(highwaySpeed)

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

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

The vehicle’s own mass is accessed using self.mass

Uses the objects own attibutes to calculate it’s energy.

Page 4: Lecture+20+-+Object+Oriented+Programming

4

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>>>

Page 5: Lecture+20+-+Object+Oriented+Programming

5

class Point: X = 0 Y = 0 # default coord is the origin def setCoords(self, x, y): self.X = x self.Y = y return def getCoords(self): return [self.X, self.Y]

class Circle: centre = Point() onCircle = Point() def __init__(self, cX, cY, onX, onY): # constructor method self.centre.setCoords(cX, cY) self.onCircle.setCoords(onX, onY)

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

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

Changes the internal variables of the Point

Returns the list of [x, y] coordinates

A Circle has two attributes of type point

We define how Circle is created so that it is always properly initialised

Call the setCoords method of the centre Point for this Circle

Page 6: Lecture+20+-+Object+Oriented+Programming

6

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 = 8print("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 hula hoop has radius = 5.0the hula hoop has area = 78.53981633974483the hula hoop now has radius = 8the hula hoop now has area = 201.06192982974676the point on the circle has moved to: [9, 1]>>>