Download presentation
Presentation is loading. Please wait.
Published bySheryl Jones Modified over 9 years ago
1
Chapter 17 Q and A Victor Norman, et al. CS104
2
What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects, which hold data, but also can do stuff to themselves or for the caller. Functional style: set_add(set, newItem) – Pattern: function(object, values) OO style: set.add(newItem) – Pattern: object.method(values)
3
Example: Set class (type) Old style: – set = newSet( ) – set = setAdd(set, newItem) – set = setDel(set, delItem) – tOrF = setIn(set, value) – set = setAnd(set, otherSet) – set = setOr(set, otherSet) New style: – set = Set( ) – set.add(newItem) – set.del(delItem) – tOrF = set.in(value) – set = set.and(otherSet) – set = set.or(otherSet)
4
Binding together data and ops A class definition binds together – the data an instance stores (its attributes) and – the operations on the data (called “methods”) Forces programmer to keep related code together. Allows you to create a new type.
5
Advantages of OO You can control how an object can be created and used. – because you write the code for how an object is constructed. You can hide how an object implements its operations (data hiding). – because you define what a user can do with objects, so the user doesn’t have to know how it works.
6
Example Suppose we want to define a type Scribbler, which will represent a programmable robot. We want it to be able to move around, keep track of where it is and what direction it is pointing, etc. – it might even keep track of where it saw obstacles, etc. Its attributes would be: x, y, angle, etc.
7
Example (2) To create one, we’d do this code: scrib = Scribbler() – calls the constructor code we’ve defined: class Scribbler: def __init__(self): self._x = 0 self._y = 0 self._angle = 0 # east Now, we control how a Scribbler is made and ensure its state is correct.
8
Example (3) What do we want to do a Scribbler? scrib.move(16, 12) – moves the robot there and object remembers where it is and what angle it is facing. scrib.getX() scrib.getY() scrib.getAngle() – scrib user can ask the scribbler where it is and what angle it is facing.
9
Example (4) scrib.seeObstacle() – ask the scribbler if it sees an obstacle. scrib.forwardUntilSeeObstacle() scrib.setAngle(newAngle) – turns the scribbler to that angle scrib.setY(newY) – moves the scribbler to new position
10
String representation (Review: when constructor is called, __init__() is run in the class.) When you do str(object), __str__ is run in the object’s class, if defined. – it must return a string, which should be a readable representation of that object. Very convenient to be able to do: print scrib # which calls str(scrib) before printing
11
__str__ code class Scribbler:... init method … def __str__(self): return “Scribbler at (%g, %g), facing %g degrees” % (self._x, self._y, self._angle) print scrib output: Scribbler at (3.0, 4.0), facing 45.0 degrees
12
self parameter if we want to do scrib.getAngle(), we implement: …. in Scribbler class …. def getAngle(self): return self._x Notice that scrib self. Object before the. is the first parameter in the method definition. self is the name of the object in code in the class.
13
Operator Overloading if I do 3 + 4, we are adding integers. if I do “hi” + “there” we are concatenating strings. – This is overloading of the operator +. for a Set type, we might want to do this: s = Set(…) s = s + 3# add 3 to the set. We’d have to implement __add__ in Set class.
14
Other operators to overload Common operators to overload: – __cmp__(self, other): compares two objects. Return -1 if self other. (If implemented, you can sort objects.) – __len__(self): can use len(obj). Useful, e.g., for Set type to ask how many items are in the set. – __contains__(self, item): if implemented, can ask if something is in the object.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.