MIT6 0001F16 Object Oriented Programming
OBJECT ORIENTED PROGRAMMING
(download slides and .py filesfollow along!)
6.0001 LECTURE 8
6.0001 LECTURE 8
1
OBJECTS
Python supports many different kinds of data
1234
3.14159
"Hello"
[1, 5, 7, 11, 13]
{"CA": "California", "MA": "Massachusetts"}
each is an object, and every object has:
? a type ? an internal data representation (primitive or composite) ? a set of procedures for interaction with the object
an object is an instance of a type
? 1234 is an instance of an int ? "hello" is an instance of a string
6.0001 LECTURE 8
2
OBJECT ORIENTED PROGRAMMING (OOP)
EVERYTHING IN PYTHON IS AN OBJECT (and has a type)
can create new objects of some type
can manipulate objects
can destroy objects
? explicitly using del or just "forget" about them ? python system will reclaim destroyed or inaccessible
objects ? called "garbage collection"
6.0001 LECTURE 8
3
WHAT ARE OBJECTS?
objects are a data abstraction that captures...
(1) an internal representation
? through data attributes
(2) an interface for interacting with object
? through methods (aka procedures/functions)
? defines behaviors but hides implementation
6.0001 LECTURE 8
4
EXAMPLE: [1,2,3,4] has type list
how are lists represented internally? linked list of cells
L = 1 ->
2 ->
3 ->
4 ->
how to manipulate lists?
? L[i], L[i:j], + ? len(), min(), max(), del(L[i]) ? L.append(),L.extend(),L.count(),L.index(),
L.insert(),L.pop(),L.remove(),L.reverse(), L.sort()
internal representation should be private
correct behavior may be compromised if you manipulate internal representation directly
6.0001 LECTURE 8
5
ADVANTAGES OF OOP
bundle data into packages together with procedures that work on them through well-defined interfaces
divide-and-conquer development
? implement and test behavior of each class separately ? increased modularity reduces complexity
classes make it easy to reuse code
? many Python modules define new classes ? each class has a separate environment (no collision on
function names) ? inheritance allows subclasses to redefine or extend a
selected subset of a superclass' behavior
6.0001 LECTURE 8
6
Implementing the class
Using the class
CREATING AND USING YOUR
OWN TYPES WITH CLASSES
make a distinction between creating a class and using an instance of the class
creating the class involves
? defining the class name ? defining class attributes ? for example, someone wrote code to implement a list class
using the class involves
? creating new instances of objects ? doing operations on the instances ? for example, L=[1,2] and len(L)
6.0001 LECTURE 8
7
Implementing the class
Using the class
DEFINE YOUR OWN TYPES
use the class keyword to define a new type
class Coordinate(object):
#define attributes here
similar to def, indent code to indicate which statements are part of the class definition
the word object means that Coordinate is a Python object and inherits all its attributes (inheritance next lecture)
? Coordinate is a subclass of object ? object is a superclass of Coordinate
6.0001 LECTURE 8
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
Related searches
- object oriented programming in matlab
- matlab object oriented programming pdf
- object oriented programming tutorial pdf
- object oriented programming book pdf
- object oriented programming c pdf
- object oriented programming 2 pdf
- object oriented programming pdf download
- object oriented programming c book
- object oriented programming java examples
- object oriented programming language pdf
- object oriented programming python pdf
- c object oriented programming examples