Difference between revisions of "Object-oriented programming"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
(Terminology: Updated links)
(add example; copyedit)
Line 1: Line 1:
 
{{OOP sidebar}}
 
{{OOP sidebar}}
'''Object-oriented programming''' (OOP) is a method of programming using [[Object|objects]] that store/organize their own data and interact with each other via their methods. Programming languages that support object-oriented programming provide a built-in way to implement [[Data abstraction|data abstractions]], as classes correspond to abstract data types (ADTs).
+
'''Object-oriented programming''' (OOP) is a method of programming using [[object]]s that store/organize their own data and interact with each other via their methods. Programming languages that support OOP provide a built-in way to implement [[Data abstraction|data abstractions]], as classes correspond to abstract data types (ADTs).
  
 
== Motivation ==
 
== Motivation ==
Line 15: Line 15:
 
* dot notation
 
* dot notation
 
* bound method vs function
 
* bound method vs function
* self
+
* [[Object#Self|self]]
 
* [[init|__init__]]
 
* [[init|__init__]]
* attribute access/assignment
+
* [[Attribute|attribute access/assignment]]
** for methods
+
 
*** for the class: A.method = lambda self: "test"
+
== Adding methods after defining the class ==
*** for an instance: inst.method = lambda: "test"
+
To add a method to a class after defining the class:
 +
<syntaxhighlight lang="python">
 +
Class.method = lambda self: ret_value
 +
</syntaxhighlight>
 +
 
 +
To add a method to an instance of a class:
 +
<syntaxhighlight lang="python">
 +
inst.method = lambda: "test"
 +
</syntaxhighlight>
 +
 
 +
Examples:
 +
<syntaxhighlight lang="python">
 +
>>> class A:
 +
...    def __init__(self, x):
 +
...        self.x = x
 +
...
 +
>>> A.prev = lambda self: self.x - 1
 +
>>> a = A(1)
 +
>>> a.prev()
 +
0
 +
</syntaxhighlight>
 +
 
 +
>>> class A:
 +
...    def __init__(self, x):
 +
...        self.x = x
 +
...    def next(self):
 +
...        return self.x + 1
 +
...
 +
>>> a = A(1)
 +
>>> a.prev = lambda: a.x - 1
 +
>>> a.prev()
 +
0
  
 
== Inheritance ==
 
== Inheritance ==

Revision as of 12:54, 24 July 2014

Object-oriented programming (OOP) is a method of programming using objects that store/organize their own data and interact with each other via their methods. Programming languages that support OOP provide a built-in way to implement data abstractions, as classes correspond to abstract data types (ADTs).

Motivation

Terminology

Syntax

Adding methods after defining the class

To add a method to a class after defining the class:

Class.method = lambda self: ret_value

To add a method to an instance of a class:

inst.method = lambda: "test"

Examples:

>>> class A:
...     def __init__(self, x):
...         self.x = x
... 
>>> A.prev = lambda self: self.x - 1
>>> a = A(1)
>>> a.prev()
0

>>> class A: ... def __init__(self, x): ... self.x = x ... def next(self): ... return self.x + 1 ... >>> a = A(1) >>> a.prev = lambda: a.x - 1 >>> a.prev() 0

Inheritance

Main article: Inheritance

Sources