Difference between revisions of "Object-oriented programming"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
m (fix)
m (remove content; use this article as a way to link to other OOP topics)
Line 1: Line 1:
 +
{{Sufficient}}
 
{{OOP sidebar}}
 
{{OOP sidebar}}
 
'''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).
 
'''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 ==
 
* [[Data abstraction|data abstraction/ADT]]
 
 
== Terminology ==
 
* [[Object]]
 
* [[Class]]
 
* Instance - a single occurrence of a class as an object
 
* [[Attribute#Method|Method]]
 
* [[Attribute]]
 
 
== Syntax ==
 
* dot notation
 
* bound method vs function
 
* [[Object#Self|self]]
 
* [[init|__init__]]
 
* [[Attribute|attribute access/assignment]]
 
 
=== Adding methods after defining the class ===
 
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>
 
 
<syntaxhighlight lang="python">
 
>>> class A:
 
...    def __init__(self, x):
 
...        self.x = x
 
...
 
>>> a = A(1)
 
>>> a.prev = lambda: a.x - 1
 
>>> a.prev()
 
0
 
</syntaxhighlight>
 
 
== Inheritance ==
 
{{Main|Inheritance}}
 
  
 
== Sources ==
 
== Sources ==

Revision as of 15:01, 24 July 2014

Template:Sufficient

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).

Sources