Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 19-21 Object Oriented Programming (OOP) CSC1310 Fall 2009.

Similar presentations


Presentation on theme: "Chapter 19-21 Object Oriented Programming (OOP) CSC1310 Fall 2009."— Presentation transcript:

1 Chapter 19-21 Object Oriented Programming (OOP) CSC1310 Fall 2009

2 Objects as Models object-based. Code is object-based. A program can be thought of as a model of reality, with objects in the program representing physical objects. Properties of objects:  State  State (information stored within the object)  Behavior  Behavior (operations that can be performed on the object)

3 Example 1: Ball-point Pen state The state of a ball-point pen with a retractable point can be represented by two values:  Is the point of the pen exposed?  How much ink remains in the pen? Operations Operations on a pen include:  Press the button at the end of the pen.  Move the pen with the point held against a sheet of paper.  Replace the pen’s cartridge.  Determine how much ink remains in the pen.

4 Example 2: Bank Account state A state of a bank account includes the account number, the balance, the transactions performed on the account since it was opened, and so forth. Operations Operations on a bank account include:  Deposit money into an account.  Withdraw money from the account.  Check the balance in the account.  Close the account.

5 Example 3: Car state The state of a car includes the amount of fluids in the car, the state of the tires, and even the condition of each part in the car. For programming purposes, we can focus on just a few elements of the state:  Is the engine on?  How much fuel remains in the car’s tank? Operations Operations on a car include:  Start the engine.  Drive a specified distance.

6 Artificial Objects Nearly every “real-world” object can be modeled within a program. Programmers also work with artificial objects that don’t correspond to objects in the physical world (mathematical objects: function, fraction, data record) Like all objects, these artificial objects have state and behavior.

7 What is OOP Python OOP is entirely optional! object-oriented To qualify as being truly object-oriented objects generally need to also participate in something called an inheritance hierarchy. Inheritance objectsInheritance is a mechanism of code customization and reuse. It is a way to form new classes (instances of which are called objects) using classes that have already been defined.

8 What is OOP Polymorphism Polymorphism means that meaning of operation depends on the object being operated on. Encapsulation Encapsulation means packaging in Python: methods and operators implement behavior; data hiding is a convention by default.

9 Why Classes Class Class is for implementing new kind of objects (state+behavior). Multiple Instances Multiple Instances  Class = factory for generating objects.  When class is called: new object is generated with its namespace. It has access to class attributes, but keeps own data. Customization via Inheritance Customization via Inheritance  Classes are extended by redefining their attributes outside class itself (namespace hierarchy) Operator Overloading Operator Overloading  New class can implement built-in type operations.

10 Attribute Inheritance Search Object.attributefirst attributeobject Object.attribute:”find the first occurrence of attribute by looking in object, and all classes above it, from bottom to top and left to right”. To find an attribute, search a tree Inheritance Inheritance: objects lower in a tree inherit attributes of the objects higher in a tree Classes Classes - instance’s factory: provide default behavior Instances Instances – concrete items: record data that varies per specific object.

11 Class tree superclasses (parents) subclass override Search is bottom-up: subclass may override parent’s behavior, by redefining the attribute (for, example C1.x) I2 “inherits” w from C3. I1.x, I2.x get from C1. I1.z, I2.z find z in C2. I2.name finds name in I2. namespacesClasses and instances are namespaces. Objects in class tree have “automatically-search” links to other namespace objects.

12 Coding the Class Tree class class statement generates a new class class (superclass,…): data=value #shared class data data=value #shared class data def (self,…): # Methods def (self,…): # Methods self.status=data self.status=data Each time a class is called, new instance is created. Instances are automatically linked to the class they are created from Classes are linked to their superclasses. Left-to-right order in parenthesis gives the order in tree.

13 Coding the Class Tree class class C2: self def x(self): print self.account self def z(self,data): self.acc=data class class C3: self def w(self,data): self.acc+=data self def z(self,data): self.acc-=data class class C1 (C2,C3): self def x(self,data): self.acc-=data self def y(self, time): print “at ”+time+”:”+self.name +” has ”+ str(self.acc)I1=C1() I1.name=“O’Reily” I1.z(123) I1.y(“09/10/08”)I2=C2()

14 Self defmethod def inside class is called as method self Method automatically receives a special first argument – self - which provides a handle back to the instance to be processed. class class C1: self def setname(self,name): self.name=name def printname(self): print self.name I1=C1(); I2=C1(); I1=C1(); I2=C1(); I1.setname(“Genry”); I2.setname(“Garry”) I1.printname(); I2.printname() When method assigns to self attributes, it creates or changes an attribute of the instance being processed.

15 __init__, __init__ constructor If coded and inherited, __init__ is called automatically each time an instance is generated (constructor). Ensure that attributes are always set in instances without requiring extra calls. class class C1: __init__ def __init__(self, name): self.name=name def printname(self): print self.name I1=C1(“Genry”); I2=C1(“Garry”) I1.printname(); I2.printname()

16 Class Objects: Default Behavior class The class statement creates a class object and assigns it a name. class Assignments inside class statement make class attributes. Class attributes provide object state and behavior:  Record state and behavior to be shared among instances  Methods process instances. class class C1: state=value self def __init__(self,name): self.name=name def printName(self): print self.name

17 Instance Objects: Concrete Items Calling a class object makes a new instance object. Each instance object inherits class attributes and gets its own namespace. self Assignments to attributes of self in methods make per- instance attributes.

18 Example 1 x.data=“change explicitly” x.newAttr=“less common:)”

19 Hierarchy of Classes Subclasses allow to change default behavior without changing component in place. Subclasses allow to have different implementation of the same method depending on the object. Subclass may replace inherited attributes completely, provide attributes that superclass expects to find, extend superclass methods. class Classes inherit attributes from their superclasses listed in parenthesis in the header of class. Instances inherit attributes from all accessible classes. object.attrribute Each object.attrribute invokes a new search. subclassing Logic changes are made by subclassing, not by changing superclasses.

20 Example2 Specialization of the second class is external for the first, it does not affect it.

21 Calling Superclass Constructor __init__ __init__ is looked up by inheritance If subclass constructor need to guarantee that superclass constructor runs too, it needs explicitly call it. class Parent: def __init__(self, x):… class Child(Parent): def __init__(self, x,y): Parent.__init__(self,x) … Without explicit call, subclass constructor replaces superclass constructor completely.


Download ppt "Chapter 19-21 Object Oriented Programming (OOP) CSC1310 Fall 2009."

Similar presentations


Ads by Google