Week 7 Lecture: Programming

Week 7 Lecture: Introduction to Object Oriented Programming Introduction to Programming for GIS & Remote Sensing GEO6938-4172 GEO4938 4166 GEO4938-41...
Author: Cleopatra Todd
2 downloads 3 Views 118KB Size
Week 7 Lecture: Introduction to Object Oriented Programming Introduction to Programming for GIS & Remote Sensing

GEO6938-4172 GEO4938 4166 GEO4938-4166

Objects: You’ve You ve Been Exposed! Modularization you know about it Modularization, it… – Methods – Procedures

Objects: Thi Obj Thinkk off them h as modularizing d l i i modules…

First, Objects in Action Object: Instance of a class Cl Class:

Defines D f the h possible bl operations and d states of an object

Each instance of a class ((each object) j ) is independent of all others!

Other Object Oriented Definitions Encapsulation: – Each E h instance i manages its i own state

Polymorphism: – Same method call can invoke one of many possible methods, dependent only on object type

Inheritance: – Define new classes by extending old ones

Reflection: – Programs can inspect themselves (specific to certain languages)

Classes in Python A Python script may define many classes:

Pyttho on

– Start code block using class keyword – Class contents are indented:

Classes Contain Methods and Variables Create instance variables by assignment to self.varname within a method No declaration, just use! – And yes, this means you can add members any time you want

With ggreat power p comes great g responsibility! p y

Instance Variables Explained

Pyttho on

“self” self refers to one particular instance of an object, so self.value can be different for every instance of Counter:

In Addition to “Instance” Variables, Classes Have “Class” Variables

Pyttho on

– Defined at the class level – Does not use the self keyword because self implies object j or instance relevance

Instance or Class Variables Can Be Objects!!! In Python (and this is key) everything is an object! So it should come as no surprise that objects and d instances off classes l can contain references f to other objects.

Inheritance You can extend a parent class to create a child by putting parents name in paren paren’ss

Pyttho on

To get all of the functionality of the parent constructor, __init__(), you must call it!

Overriding the Parent Stepper has all the functions and instance variables defined by Counter… Counter

Pyttho on

But it also overrides the step() method to decrement instead of increment, in addition to adding the tripper() function.

Relationships Between Classes There’ss a basic distinction to understand: There

“Is a…” vs. “Has a…”

Take simple 2D shapes for example: – Class definition for a point – The Point instances will have two instance variables:

Pyttho on

• self.x • self.y

Take simple 2D shapes for example:

Pyttho on

– Class definition for a line – The Line instances will have point objects as instance variables

Not just about __init__(), Other Double-Underscore Functions Specially-named p y methods for everyy arithmetic operator: p – __add__ for + – __mul__ for *

SSo if x is i an object, bj x+2 +2 is i really ll x.__add__(2) dd (2) Operators also have right-hand methods (e.g. __radd__()): So 2 2+xx is x.__radd__(2) x. radd (2)

Execution order for a+b is: – If a has a method __add__, call a.__add__(b) – If b has a method __radd__, call b.__radd__(a) – Else use Python Python’ss built built-in in default

So imagine overriding the + operator for our Point object:

Pyttho on

We can now add points because we’ve defined a method for the + operator when Point objects are involved: l d

So imagine overriding the + operator for our Point object:

We defined __add__(), add (), now “+” works…

Pyttho on

“+” + isn’t isn t defined yet

Visualizing Objects and Class Relationships – UML Is a.. a

Has a.. H

In Lab This Week We’ll continue exploring different methods for modularization d l i i through h h the h use off basic b i object bj oriented programming Plus.. wrapping pp g upp the Valentine’s Dayy friendlyy coding competition! Supplemental reading: – http://www.diveintopython.org/object_oriented_framework/index.html http://www diveintopython org/object oriented framework/index html