06-Intro-Object-Oriented-Prog.ppt: uploaded 1 April 2016 at 4:01 pm

Download Report

Transcript 06-Intro-Object-Oriented-Prog.ppt: uploaded 1 April 2016 at 4:01 pm

Introduction to Object-Oriented
Programming in Python
Barb Ericson
Georgia Institute of Technology
July 2009
06-Intro-Object-Oriented-Prog
1
Learning Goals
• Compare procedural programming and
object-oriented programming
• Introduce computation as simulation
• Create objects in Python
• Invoke methods on objects in Python
• Create a subclass
• Create a method in a class
• Pass a parameter to a method in a class
06-Intro-Object-Oriented-Prog
2
Procedural Programming
• Focus is on the procedures
– The tasks that need to be done
– Break large tasks into smaller tasks
• Write a function for each task
• Problems with procedural
– The data is passed around and any function
can change the data
– It is hard to find the function
• They can be in many files with many names
06-Intro-Object-Oriented-Prog
3
Object-Oriented Programming
• The focus is on who does each task as
well as the tasks to be done
– Responsibility driven
• Advantages
– Manage complexity by distributing
responsibility across objects
– Make systems robust by making objects work
independently
– Support reuse because objects provide
services
06-Intro-Object-Oriented-Prog
4
Computers as Simulators
• “The computer is the Proteus of machines.
Its essence is its universality, its power to
simulate. Because it can take on a
thousand forms and serve a thousand
functions, it can appeal to a thousand
tastes.” Seymour Papert in Mindstorms
06-Intro-Object-Oriented-Prog
5
Creating a Simulation
• Computers let us simulate things
– We do this by creating models of the things
we want to simulate
– We need to define what types of objects we
will want in our simulation and what they can
do
• Classes define the types and create objects of that
type
• Objects act in the simulation
06-Intro-Object-Oriented-Prog
6
Creating objects
• We have been creating objects in Python
using
– makePicture(file)
– makeSound(file)
• But, these are functions we created to
make it easy to create objects from these
classes
06-Intro-Object-Oriented-Prog
7
Creating objects in python
• Use ClassName(value1,value2,…)
– Picture(file) # create a picture
– Sound(file) # create a sound
– World()
# create a world
• Name the objects that are created
>>> earth = World()
>>> tina = Turtle(earth)
>>> sue = Turtle(earth)
• Use the name to ask the object to do something
objName.function()
>>> tina.forward()
06-Intro-Object-Oriented-Prog
8
Turtle Behaviors
•
•
•
•
•
•
•
•
•
•
•
forward() # forward by 100 pixels
forward(amount) # forward by amount
turnLeft() # 90 degree left
turnRight() # 90 degree right
turn(degrees) # pos right and neg is left
penUp() # pick up the pen
penDown() # put pen down
moveTo(x,y) # move to x and y location
setColor(blue) # set the color
setPenWidth(width) # size of pen trail
setVisible(false) # don't draw turtle
06-Intro-Object-Oriented-Prog
9
Challenge
• Use a turtle to draw a square
• Use a turtle to draw blocks of color using a
wide pen trail
06-Intro-Object-Oriented-Prog
10
Create a Subclass
class SmartTurtle(Turtle):
def drawSquare(self):
for i in range (0 ,4):
self.turnRight ()
self.forward ()
>>> earth1 = World()
>>> smarty = SmartTurtle(earth1)
>>> smarty.drawSquare()
06-Intro-Object-Oriented-Prog
11
Passing Parameters to Methods
class SmartTurtle(Turtle):
def drawSquare(self):
for i in range (0 ,4):
self.turnRight ()
self.forward ()
>>> mars = World ()
>>> tina = SmartTurtle(mars)
>>> tina.drawSquare (30)
>>> tina.drawSquare (150)
>>> tina.drawSquare (100)
def drawSquare(self, width):
for i in range (0 ,4):
self.turnRight ()
self.forward(width)
06-Intro-Object-Oriented-Prog
12
Creating the Slide Class
class Slide:
def show(self):
show(self.picture)
blockingPlay(self.sound)
>>> slide1=Slide()
>>> slide1.picture =
makePicture(getMediaPath("barbara.jpg"))
>>> slide1.sound =
makeSound(getMediaPath("bassoon-c4.wav"))
>>> slide1.show()
06-Intro-Object-Oriented-Prog
13
Constructors
class slide:
def __init__(self , pictureFile ,soundFile ):
self.picture = makePicture(pictureFile)
self.sound = makeSound(soundFile)
def show(self ):
show(self.picture)
blockingPlay(self.sound)
06-Intro-Object-Oriented-Prog
14
Testing the constructor
def playSlideShow2 ():
pictF = getMediaPath("barbara.jpg")
soundF = getMediaPath("bassoon -c4.wav")
slide1 = slide(pictF ,soundF)
pictF = getMediaPath("beach.jpg")
soundF = getMediaPath("bassoon -e4.wav")
slide2 = slide(pictF ,soundF)
pictF = getMediaPath("church.jpg")
soundF = getMediaPath("bassoon -g4.wav")
slide3 = slide(pictF ,soundF)
pictF = getMediaPath("jungle2.jpg")
soundF = getMediaPath("bassoon -c4.wav")
slide4 = slide(pictF ,soundF)
slide1.show ()
slide2.show ()
slide3.show ()
slide4.show ()
06-Intro-Object-Oriented-Prog
15
Object-oriented with Pictures
>>> pic=Picture(getMediaPath("barbara.jpg"))
>>> pic.show()
>>> pic.getWidth()
>>> pic.getHeight()
>>> pixels = pic.getPixels()
>>> pixel = pic.getPixel(0,0)
>>> color = pixel.getColor();
>>> red = pixel.getRed()
06-Intro-Object-Oriented-Prog
16
Picture Functions
def show(picture ):
if not picture.__class__ == Picture:
print "show(picture ): Input is not a picture"
raise ValueError
picture.show ()
06-Intro-Object-Oriented-Prog
17
Summary
• Procedural programming has a focus on the
procedures (tasks)
• Object-oriented programming has a focus on the
objects and on simulation
• You can create objects from classes in Python
– Picture(file) or Class(parameters)
• You can create subclasses in Python
– class SmartTurtle(Turtle):
• You can overload methods by passing different
parameters
06-Intro-Object-Oriented-Prog
18