Objects
CptS 355 - Programming Language Design Washington State University |
|
Object-oriented Programming and Languages(This is chapter 10; we will come back to chapter 9)Initial OO insight: data and operations belong togetherWe can associate data and operations in one data structure. Such data structures have come to be called objects. Another way to say it: associate state and behavior in one data structure.Two schools of terminology:
Second OO insight: abstraction / interfaceClients/users of an object should not have access to its state except via a defined interface - a set of operations often expressed in application level terms rather than implementation level terms. Example:
Third OO insight: subtypingOnce we begin to discuss interfaces, we realize:
Fourth insight: Dynamic dispatchSuppose: v1 is value of type I1 and v2 is a variable of type I2 and y is a variable of type I2 where I1 <: I2. By the definition of subtyping
if(...) {
y := (v1: I1)
} else
y := (v2:I2)
}
is legal because any
value of a subtype can be used
whenever a value of its supertype can be used.
Suppose further that both types
I1 and I2 have methods named Which Fifth insight: InheritanceProper use of subtyping requires values of a subtype to implement the operations of its supertype with similar meaning. What is an easy way to accomplish this? Let the operations of the subtype be the same as the operators of the supertype. (There are lots of fine points here: such attributes like private, protected, public, etc.) Note well that inheritance is a property of implementations while subtyping is a property of interfaces:
(C++ subclassing also allows restricting (narrowing) the interface which means a C++ subclass may not be a subtype of its superclass). Once a class is defined, objects that have that type are called instances of the class. Warning: on p. 284, for "typed languages" read "statically typed languages" and for "untyped languages" read "dynamically typed languages". Object-oriented PythonBasic syntax:class classname [(supername [,supername]+)]: suite In the suite put method function definitions.
Each method's first parameter inside a class
is always
class classname [(supername [,supername]+)]:
def __init__(self, p1, p2,pn):
suite
def m1(self, p1, p2):
suite
def m2(self, p1):
suite
In Python all method names must be unique -- there is no ad-hoc
polymorphism based on method name overloading.
To create a new instance of a Python object write className(parameters of the __init__ method)The __init__
method is special: it is called immediately after a new object is
constructed and is typically used to initialize the member data of the
object.
Here is a simple, non-sensical class illustrating some of these features.
class myclass(parent):
imAStaticMember = 37
def __init__(p1):
parent.__init__(self)
self.a1 = 1
self.a2 = "x"
self.a3 = p1
def update(self,p1):
self.a3 = p1
Note: data members of instances are created in the __init__ method or by subsequent assignment. Class-level data members are created in the class suite itself and act like static members in C++. |
|