mark dixon page 1 13 – object oriented programming
Post on 21-Dec-2015
217 views
TRANSCRIPT
Mark Dixon Page 1
13 – Object Oriented Programming
Mark Dixon Page 2
Admin: Coursework 3 – Test• In class test
– 26 Jan 2009: teaching week 15
• 50 mins
• short answer (5 - 6 words max)
• 25% of coursework mark
Mark Dixon Page 3
Questions: Functions• Consider the following code: Function Smallest(num1, num2) If num1 < num2 Then Smallest = num1 Else Smallest = num2 End If End Function
• name a function.• what is left in small after the following is
executed?Dim small small = Smallest(23, 15) 15
Smallest
Mark Dixon Page 4
Session Aims & Objectives• Aims
– To introduce the fundamental ideas of object orientation
• Objectives,by end of this week’s sessions, you should be able to:
– create a class definition, which includes• properties, and• methods
– create an object instance, for the class– use the object instance, by
• assigning values to its properties, and• calling its methods
Mark Dixon Page 5
Example: Ball Bounce v1<html> <head><title>Ball Bounce</title></head> <body style="margin: 0;"> <img id="imgBall" src="Ball.gif" style="position: absolute;" /> </body></html>
<script language="vbscript">Option ExplicitDim xDim yDim xIncDim yInc
Sub window_onLoad() window.setinterval "Main", 20 xInc = 5 yInc = 3 End Sub
Sub Main() x = imgBall.style.pixelLeft + xInc If x <= 0 Or x >= document.body.clientWidth - imgBall.width Then xInc = -xInc Else imgBall.style.pixelLeft = x End If y = imgBall.style.pixelTop + yInc If y <= 0 Or y >= document.body.clientHeight - imgBall.height Then yInc = -yInc Else imgBall.style.pixelTop = y End If End Sub</script>
Mark Dixon Page 6
Structured Paradigm• Program made up of
– data structures, and – routines (procedures and functions) that
process the data within those structures.
• Each routine should perform a single, clearly identifiable operation.
• Each routine should be self-contained
• Abstract data type = structure + procedures
Mark Dixon Page 7
Example: Ball Bounce v2Option ExplicitDim xDim yDim xIncDim yInc
Sub Init(tmpXInc, tmpYInc) xInc = tmpXInc yInc = tmpYIncEnd Sub
Sub Move(img) x = img.style.pixelLeft + xInc If x <= 0 Or x >= document.body.clientWidth - img.width Then xInc = -xInc Else img.style.pixelLeft = x End If y = img.style.pixelTop + yInc If y <= 0 Or y >= document.body.clientHeight - img.height Then yInc = -yInc Else img.style.pixelTop = y End IfEnd Sub
<html> <head><title>Ball Bounce</title></head> <body style="margin: 0;"> <img id="imgBall" src="Ball.gif" style="position: absolute;" /> </body></html>
<script language="vbscript" src="Sprite.vbs"></script><script language="vbscript">Option Explicit
Sub window_onLoad() window.setinterval "Main", 20 Init 5, 3 End Sub
Sub Main() Move imgBall End Sub</script>
BallBounce.htm
Sprite.vbs
Mark Dixon Page 8
Object-Oriented Paradigm• program made up of objects that communicate
with each other by passing messages
• Each object contains– attributes/properties that represent its state, and– operations/methods that represent its behaviour
• Objects often mirror the real world– Customers– Students– Patients
Mark Dixon Page 9
Classes and Instances• Object Classes
– general descriptions of types of objects,e.g. student, product, customer, lecturer, and room.
• Object Instances– specific items of a given class, e.g.
• each of you could be an instance of the student class• Room 214 could be an instance of the room class• I could be an instance of the lecturer class• Bolt could be an instance of the part class
Mark Dixon Page 10
Object Concepts - Implementation
• Properties – implemented as– data structures (variables, and arrays)
• Methods – implemented as either– a procedure (to perform some processing), or– a function (to return a value)
• Object oriented paradigm builds on (rather than replaces) the structured paradigm
Mark Dixon Page 11
Example: Animals
• Class: Animal• Properties: Name, Species, Gender• Instances: myPet, yourPet
Mark Dixon Page 12
Question: Objects• Consider the following code: Class FetalBloodSample Dim Time Dim Acidity Dim Reactivity End Class Dim fbs1 Set fbs1 = New FetalBloodSample
• Name all:– classes– properties– instances
FetalBloodSample
Time, Acidity, Reactivity
fbs1
Mark Dixon Page 13
Example: Ball Bounce v3Option Explicit
Class Sprite Dim x Dim y Dim xInc Dim yIncEnd Class
Sub Init(spr, tmpXInc, tmpYInc) spr.xInc = tmpXInc spr.yInc = tmpYIncEnd Sub
Sub Move(spr, img) spr.x = img.style.pixelLeft + spr.xInc If spr.x <= 0 Or spr.x >= document.body.clientWidth - img.width Then spr.xInc = -spr.xInc Else img.style.pixelLeft = spr.x End If spr.y = img.style.pixelTop + spr.yInc If spr.y <= 0 Or spr.y >= document.body.clientHeight - img.height Then spr.yInc = -spr.yInc Else img.style.pixelTop = spr.y End IfEnd Sub
<html> …</html>
<script language="vbscript" src="Sprite.vbs"></script><script language="vbscript">Option ExplicitDim ball
Sub window_onLoad() window.setinterval "Main", 20 Set ball = New Sprite Init ball, 5, 3 End Sub
Sub Main() Move ball, imgBall End Sub</script>
BallBounce.htm
Sprite.vbs
Class Definition
Mark Dixon Page 14
Example: Students
• Method: Clear
Mark Dixon Page 15
Example: Ball Bounce v4Option Explicit
Class SpriteDim xDim yDim xIncDim yInc
Sub Init(tmpXInc, tmpYInc) xInc = tmpXInc yInc = tmpYInc End Sub
Sub Move(img) x = img.style.pixelLeft + xInc If x <= 0 Or x >= document.body.clientWidth - img.width Then xInc = -xInc Else img.style.pixelLeft = x End If y = img.style.pixelTop + yInc If y <= 0 Or y >= document.body.clientHeight - img.height Then yInc = -yInc Else img.style.pixelTop = y End If End SubEnd Class
<html> …</html>
<script language="vbscript" src="Sprite.vbs"></script><script language="vbscript">Option ExplicitDim ball
Sub window_onLoad() window.setinterval "Main", 20 Set ball = New Sprite ball.Init 5, 3 End Sub
Sub Main() ball.Move imgBall End Sub</script>
BallBounce.htm
Sprite.vbs
• procedures are now methods inside the class
Mark Dixon Page 16
Example: Ball Bounce v5• Multiple instances:
Mark Dixon Page 17
Example: Ball Bounce v5<html> <head><title>Ball Bounce</title></head> <body style="margin: 0;"> <img id="imgBall" src="Ball.gif" style="position: absolute;" /> <img id="imgFace" src="BallChar2.GIF" style="position: absolute;" /> </body></html>
<script language="vbscript" src="Sprite.vbs"></script><script language="vbscript">Option ExplicitDim ballDim face
Sub window_onLoad() window.setinterval "Main", 20 Set ball = New Sprite ball.Init 5, 3 Set face = New Sprite face.Init 1, 12 End Sub
Sub Main() ball.Move imgBall face.Move imgFace End Sub</script>
• Now have easy way of:– creating &
using multiple sprites
– each with own identity (separate characteristics)
– only a few (4) lines of code
Mark Dixon Page 18
Questions: OOP
• Name a– class– property– method– instance
Class House Dim number Dim road Dim district
Sub ChangeDist(newDist) district = newDist End SubEnd Class
Dim h Set h = New House
Housenumber, road, district
ChangeDisth
Mark Dixon Page 19
OOP: Errors• Object doesn't support this property or
method:
Class Animal Dim name Dim speciesEnd Class
Dim aSet a = New Animala.name = "Skippy"a.specys = "Kangaroo"
Mark Dixon Page 20
Private• make properties and methods invisible
outside class:
Class Counter Private value
Sub Reset() value = 0 End Sub
Sub Increase() value = value + 1 End Sub
Function GetValue() GetValue = value End FunctionEnd Class
Dim cSet c = New Counterc.Resetc.IncreaseparRes.innerText = c.GetValue()c.value = -6
Mark Dixon Page 21
Benefits of OOP in code• Procedures and Functions are part of object
– encapsulation
• Related Data and Operations together
• Private keyword – restrict access to data
• Clearer code• Reduces chance of accidental interference• Less prone to error
Mark Dixon Page 22
Example: Balloon Shoot• Question:
– what objects?
– what classes?
– what properties?
– what methods
Mark Dixon Page 23
Tutorial Exercise: Ball Bounce• Learning Objective: To create and use your own class.
• Task 1: Get the Ball Bounce examples (1, 2, and 5) from the lecture working.
• Task 2: Add a hit method to the sprite class, which detects the collision with another sprite.
• Task 3: Modify your page to count the number of hits between the two sprites.
• Task 4: Modify your page to make sprites bounce off each other.
• Task 5: Add another sprite.
Mark Dixon Page 24
Tutorial Exercise: Balloon Shoot• Learning Objective: To create and use your own classes.
• Task 1: Create the Balloon Shoot example (from the lecture) using object oriented concepts (classes, properties, methods, and instances)hint: use some of the code from your Interceptor example (from last week)