Professional Documents
Culture Documents
Chapter 7 A 2
Chapter 7 A 2
Seif Haridi
Peter Van Roy
State (attributes)
procedures (methods)
class ClassVariable
attr
AttrName1
:
AttrNameN
meth Pattern1 Statement end
:
meth PatternN Statement end
end
Copyright 2002 by S. Haridi and P. Van Roy 9
Classes (syntax simplified)
A class is also a value that can be in an expression position
class $
attr
AttrName1
:
AttrNamen
meth Pattern Statement end
:
meth Pattern Statement end
end
Copyright 2002 by S. Haridi and P. Van Roy 10
Classes in Oz
The class Counter has the syntactic form
class Counter
attr val
meth browse
{Browse @val}
end
meth inc(Value)
val := @val + Value
end
meth init(Value)
val := Value
end
end
Copyright 2002 by S. Haridi and P. Van Roy 11
Attributes of Classes
The class Counter has the syntactic form
class Counter val is an attribute
attr val a modifiable cell
meth browse
that is access by the
{Browse @val}
end atom val
meth inc(Value)
val := @val + Value
end
meth init(Value)
val := Value
end
end
Copyright 2002 by S. Haridi and P. Van Roy 12
Attributes of classes
The class Counter has the syntactic form
class Counter
attr val
meth browse
{Browse @val} the attribute val
end is accessed by the
meth inc(Value)
operator @val
val := @val + Value
end
meth init(Value)
val := Value
end
end
Copyright 2002 by S. Haridi and P. Van Roy 13
Attributes of classes
The class Counter has the syntactic form
class Counter
attr val
meth browse
{Browse @val} the attribute val
end is assigned by the
meth inc(Value)
operator :=
val := @val + Value
end as val := ...
meth init(Value)
val := Value
end
end
Copyright 2002 by S. Haridi and P. Van Roy 14
Methods of classes
The class Counter has the syntactic form
class Counter methods
attr val are statements
meth browse method head is a
{Browse @val} record (tuple) pattern
end
meth inc(Value)
val := @val + Value
end
meth init(Value)
val := Value
end
end
Copyright 2002 by S. Haridi and P. Van Roy 15
Classes in Oz
The class Counter has the syntactic form
class Counter
attr val
meth browse
{Browse @val}
end
meth inc(Value)
val := @val + Value
end
meth init(Value)
val := Value
end
end
Copyright 2002 by S. Haridi and P. Van Roy 16
Example
• The following shows how an object is created from
a class using the procedure New/3, whose first
argument is the class, the second is the initial
method, and the result is the object.
• New/3 is a generic procedure for creating objects
from classes.
declare C = {New Counter init(0)}
{C browse}
{C inc(1)}
{C browse}
class AccountWithFee
from VerboseAccount
VerboseAccount
attr fee:5
meth transfer(Amount)
...
end AccountWithFee
end
class D
O
a subclass of
an instance
C
of D
meth M
Non-Conservative
Non-Conservative extension inheritance is dangerous
class AccountWithFee because it might change
from VerboseAccount the relationship between
attr fee:5 methods and just
meth transfer(Amount) invariants the programmer
VerboseAccount, transfer(Amount - @fee) depends on
end
end
Account invariant:
getBalance(B1); transfer(S); getBalance(B2) B1+S=B2
No longer satisfied!
Copyright 2002 by S. Haridi and P. Van Roy 39
Inheritance graph
• Classes may inherit from one or several classes appearing after
the keyword from
• A class B is a superclass of a class A if:
– B appears in the from declaration of A, or
– B is a superclass of a class appearing in the from declaration
of A.
• The attributes and methods available in a class C (i.e. visible)
are defined through a precedence relation on the methods that
appear in the class hierarchy, called the overriding relation:
– A method in a class C overrides any method, with the same
label, in any super class of C.
m A (valid hierarchy)
C (invalid hierarchy)
class ListClass
… ListClass
end
class NilClass from ListClass
…
end NilClass ConsClass
class ConsClass from ListClass
…
end Copyright 2002 by S. Haridi and P. Van Roy 52
Constructing a hierarchy
by following the type II
class ListClass
meth append(_ _) raise undefinedMethod end end
end
class NilClass from ListClass declare L1 L2 L3 in
meth init skip end L1={New ConsClass
meth append(T U) U=T end init(1 {New ConsClass
end init(2 {New NilClass init})})}
class ConsClass from ListClass L2={New ConsClass
attr head tail init(3 {New NilClass init})}
meth init(H T) head:=H tail:=T end {L1 append(L2 L3)}
meth append(T U) {L3 display} % Definition not shown
U2 in
{@tail append(T U2)}
U={New ConsClass init(@head U2)}
end
end Copyright 2002 by S. Haridi and P. Van Roy 53
Abstract and concrete classes
ListClass
NilClass ConsClass
Control abstractions
class HO
meth forAll(LO M)
for O in LO do {O M} end
end
...
end
This technique allows messages as parameters