To To Get Started
- Paper sheet
- Online:
http://www.eecs.qmul.ac.uk/~william/CAS-London-2020.html
- Download sample programs
- Create directory
- Unzip
- Recommend copy sample files before editing
To To Get Started Paper sheet Online: - - PowerPoint PPT Presentation
To To Get Started Paper sheet Online: http://www.eecs.qmul.ac.uk/~william/CAS-London-2020.html Download sample programs - Create directory - Unzip - Recommend copy sample files before editing Object Oriented Programming in A Level
William Marsh [Original version co-authored with Melissa Bustamante]
design
classes and objects
relations Abstraction and Decomposition
OOP
There are many examples of classes and object that are familiar
from turtle import * from Face import Face f1 = Face(0, 0) f1.draw() f2 = Face(-200, 0) f2.setSize(80) f2.draw()
a familiar example
aware we use
This is a variable. What is the type of its value? Draw methods: which face is drawn?
Name Type Description f1, f2 Variables; Objects of ‘Face’ class A drawing of a face Face Class name; constructor Create a Face object setSize Method of ‘Face’ class Set size of the face draw Method of ‘Face’ class Draw the face ‘Method’ is an OO word for function
f2.setSize(80)
The object to be acted on The method: implements the action A second parameter: the size to set
Motivation: What are we trying to achieve with classes?
can be implemented as a single monolithic unit.
longer reasonable ...
together provide the desired function.
what kinds of modules are useful … [how] modules can be combined to solve the original problem
list)
classes
(inheritance)
classes
Key concepts
distraction
from turtle import class Face: def __init__(self, xpos, ypos): self.size = 50 self.coord = (xpos, ypos) self.noseSize = 'normal' def setSize(self, radius): self.size = radius def draw(self): ... self.drawOutline() ...
Constructor Attributes Method
from turtle import class Face: def __init__(self, xpos, ypos): self.size = 50 self.coord = (xpos, ypos) self.noseSize = 'normal' def setSize(self, radius): self.size = radius def draw(self): ... self.drawOutline() ...
def goHome(self): penup() goto(self.coord) setheading(0) def drawOutline(self): penup() forward(self.size) left(90) pendown() circle(self.size) self.goHome()
Constructor
parameters
class Face: def __init__(self, xpos, ypos): self.size = 50 self.coord = (xpos, ypos) self.noseSize = 'normal' Constructor name Constructor parameter Always ‘self’ Initialise attributes
attributes in the constructor
class Face: def __init__(self, xpos, ypos): self.size = 50 self.coord = (xpos, ypos) self.noseSize = 'normal' def setSize(self, radius): self.size = radius
in structure
If & loops
x = while x > : y = print class Friend: def __init__() def m1(a, b): class Town: def __init__() def m1(a, b):
Classes &
Main program
Functions
Function def Function def Main program
Concept Details Basic mechanics
Constructors
Interaction
relationship)
Abstraction and modelling
Inheritance
Prerequisite knowledge: functions & parameters Prerequisite knowledge: basic mechanisms
Misconception Possible Evidence
Attributes in the wrong scope
Confusion between class and
Confusion between class and attribute
Objects only contain data
Objects do not interact
Believing objects are copied not shared
Also lack of prerequisite knowledge: functions & parameters
attributes
to
Use Python to teach OOP