140 likes | 265 Views
Chapter 17 Q and A. Victor Norman, et al. CS104. 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 )
E N D
Chapter 17 Q and A Victor Norman, et al. CS104
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)
Example: Set class (type) • Old style: • set = newSet(<optional values>) • set = setAdd(set, newItem) • set = setDel(set, delItem) • tOrF = setIn(set, value) • set = setAnd(set, otherSet) • set = setOr(set, otherSet) • New style: • set = Set(<optional values>) • set.add(newItem) • set.del(delItem) • tOrF = set.in(value) • set = set.and(otherSet) • set = set.or(otherSet)
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.
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.
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.
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.
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.
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
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
__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
self parameter • if we want to do scrib.getAngle(), we implement: …. in Scribbler class …. defgetAngle(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.
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.
Other operators to overload • Common operators to overload: • __cmp__(self, other): compares two objects. Return -1 if self < other, 0 if self == other, 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.