at:tutorial:objects
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
at:tutorial:objects [2007/07/02 11:24] – jorge | at:tutorial:objects [2007/07/09 21:00] – * tvcutsem | ||
---|---|---|---|
Line 4: | Line 4: | ||
In AmbientTalk, | In AmbientTalk, | ||
classes. Rather, they are either created ex-nihilo or by cloning | classes. Rather, they are either created ex-nihilo or by cloning | ||
- | and adapting existing objects, | + | and adapting existing objects, |
The following code illustrates the ex-nihilo creation of an object: | The following code illustrates the ex-nihilo creation of an object: | ||
< | < | ||
- | > def point := object: { | + | def Point := object: { |
- | def x := 0; | + | def x := 0; |
- | def y := 0; | + | def y := 0; |
- | def init(aX,aY) { | + | def init(aX,aY) { |
- | x := aX; | + | x := aX; |
- | y := aY; | + | y := aY; |
- | }; | + | }; |
- | def sumOfSquares() { x*x + y*y }; | + | def sumOfSquares() { x*x + y*y }; |
- | } | + | } |
</ | </ | ||
- | As all definitions in AmbientTalk, objects, fields and methods are defined using the **def** keyword. Fields are defined using a '' | + | The above code defines an // |
- | <note important> | + | In the example above, the state of the point object is composed of '' |
- | AmbientTalk not only supports traditional canonical syntax (e.g. '' | + | |
- | </ | + | |
- | In the example above, the state of the '' | + | < |
+ | As already explained in the [[at: | ||
+ | |||
+ | For Smalltalk/ | ||
+ | </ | ||
===== Sending messages ===== | ===== Sending messages ===== | ||
Line 48: | Line 50: | ||
Every object understands the message '' | Every object understands the message '' | ||
+ | |||
+ | AmbientTalk also provides a '' | ||
+ | |||
+ | < | ||
+ | > def clonedPoint := clone: point | ||
+ | </ | ||
===== Delegation and cloning ===== | ===== Delegation and cloning ===== | ||
Line 61: | Line 69: | ||
> def point3D := extend: point with: { | > def point3D := extend: point with: { | ||
def z := 0; | def z := 0; | ||
- | def sumofsquares() { | + | def sumOfSquares() { |
- | super.sumofsquares() + z*z | + | super^sumOfSquares() + z*z |
} | } | ||
} | } | ||
Line 72: | Line 80: | ||
> def point3D := share: point with: { | > def point3D := share: point with: { | ||
def z := 0; | def z := 0; | ||
- | def sumofsquares() { | + | def sumOfSquares() { |
- | super.sumofsquares() + z*z | + | super^sumOfSquares() + z*z |
} | } | ||
} | } | ||
Line 82: | Line 90: | ||
< | < | ||
- | > def OpenConnection | + | > def openConnection |
- | def ClosedConnection | + | > def closedConnection |
- | def Connection | + | > def connection |
- | def open() { | + | def open() { |
- | super := OpenConnection.new(); | + | super := openConnection.new(); |
}; | }; | ||
def close() { | def close() { | ||
- | super := ClosedConnection.new(); | + | super := closedConnection.new(); |
}; | }; | ||
} | } | ||
</ | </ | ||
- | ===== First-class Delegation ===== | + | <note important> |
+ | In AmbientTalk, | ||
+ | </ | ||
+ | ===== First-class delegation ===== | ||
+ | AmbientTalk provides an explicit delegation operator '' | ||
+ | |||
+ | < | ||
+ | > def point3D := extend: point with: { | ||
+ | def z := 0; | ||
+ | def init(aX, aY, aZ) { | ||
+ | super^init(aX, | ||
+ | z := aZ; | ||
+ | }; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | A message sent to an object using the '' | ||
+ | |||
+ | <note warning> | ||
+ | The delegation operator does not have the same semantics as the dot notation. A message sent to '' | ||
+ | </ | ||
===== Encapsulation ===== | ===== Encapsulation ===== |
at/tutorial/objects.txt · Last modified: 2013/05/17 20:23 by tvcutsem