LectureThree - OOP in Action
LectureThree - OOP in Action
MATINGO
FOR GEOMATICS
[email protected]
APPLICATIONS
HSVG245
To be continued
…
OOP
IN ACTION
(APPLICATIONS OF OOP) 74
LECTURE OUTLINE
• Introducing OOP
• Difference with procedural programming
• OOP concepts
2
Image Source: TechTarget
OBJECT ORIENTED VS
PROCEDURAL
methods are used to operate on the object’s
OOP own data structure
• Centres on the use of objects that group Recall
properties and methods together • These
• The code is often broken up and distributed across multiple files, each with different
purposes
Procedural
• Separation between data and procedures
• Code executed from top to bottom
• The variables are passed to/through procedures and operated on, rather than
operating on themselves
3
OOP – WHY BOTHER?
• Useful when you want to group data and
behaviour together in pockets of
functionality
6
MAKING A SIMPLE CLASS
2D point
• What would you ask for to help you describe (define) a basic 2D point?
7
SIMPLE 2D POINT CLASS
State
Y coordinate
X coordinate Colour
8
How would you represent these states in a computer program using the Python data
types that we have already met?
SIMPLE 2D POINT CLASS (CONT’D)
State
Y coordinate
Floating point?
Colour
X coordinate
Integer?
Floating point? String?
Integer?
How to
choose which
properties to
represent the
Object by?
• Need to represent the point sufficiently to fulfil the application/program’s purpose 9
How to choose which data types to represent the property?
• Depends what operations are required to manipulate the object
ARCHITECTURE OF AN OBJECT
ORIENTED PYTHON
APPLICATION
• Modules (.py files): store logical groups of functions, constants and classes together in
the same file.
Project
Module A Functions
useful together
way modules that are
Module B Functions for
Module C Functions
• Project: a grouping
Class (Bi) Class (Ci) Cla
Any number of constants, Class (Cii)
created for use ss (Aii)
Cla specific application
ss (Ai) within a functions, classes within a Class (Bii)
module
Class (Cn) 10
Cla
ss (An) Class (Bn)
• Usually we find that modules are saved to the same location as the project • However
we may find the need to reuse (import) modules created in different projects • Typical
Python style: put related classes in the same module
• It may be that the class ends up in a module of its own (especially if the class is too
large) but this should not be the goal
• It is less confusing if the class and the module do not have the same names (different
from Java!)
• For our Point 2D example, we create a folder for our project, then a module Point.py
• Within the module we can store the Point2D class and a Point3D class if we choose to make
one11
DRIVER?
• When you create an instance of an object, python implicitly ‘calls’ the class’
constructor method to set up the state of the instance
• It passes the object as the parameter ‘self’ to the init method as well as any
other parameters required to instantiate the object
Click here to
14
CONSTRUCTOR FOR OUR SIMPLE
POINT CLASS
Class statement
must always specify
‘self’ as parameter
This dictates
that the
information to
create a
Ponint2D object, an X and Y
3. Press
You get an error if the correct
class is not imported
16
If the correct number of parameters are not
provided
ACCESSING INSTANCE VARIABLES
• There will be many occasions when we will want to access an Object’s instance
variables (i.e. those variables bound to an object within the constructor method)
from either our Driver module or other module/class
• The method considered ‘best practice’ for Python programming is to use the dot(.)
operator:
object.variable_name
• Try it yourself
17
BEHAVIOUR (METHODS)
Make a relative
movement
(translation)
Clone
Re-project
1
3
20
THE RETURN
STATEMENT
• The idea is that if we invoke a method (i.e. a function) at some point in our code,
the method ‘returns’/inserts a value to the point in the code from which we
invoked the method
• In effect, the call to the method is directly substituted by the value that the
method returns
• The return statement is allowed only inside a method/function body
• When return executes the function terminates, and the value of the expression is
the function’s result.
covered in this lecture 2. Write a driver program to: • What stages are involved??
22