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/06/27 11:34] – jorge | at:tutorial:objects [2007/06/27 12:31] – jorge | ||
---|---|---|---|
Line 9: | Line 9: | ||
< | < | ||
- | > def point := object: { | + | > def Point := object: { |
def x := 0; | def x := 0; | ||
def y := 0; | def y := 0; | ||
- | def sumofsquares() { x*x + y*y }; | + | def init(xCoord, |
+ | x := xCoord; | ||
+ | y := yCoord; | ||
+ | }; | ||
+ | def sumOfSquares() { x*x + y*y }; | ||
} | } | ||
>>< | >>< | ||
</ | </ | ||
+ | As all definitions in AmbientTalk, | ||
+ | This object can be instantiated to create new points as follows: | ||
+ | |||
+ | < | ||
+ | > def aPoint := Point.new(2, | ||
+ | >>< | ||
+ | </ | ||
+ | |||
+ | Every object understands the message '' | ||
+ | |||
+ | Object' | ||
+ | |||
+ | < | ||
+ | > aPoint.x | ||
+ | >>2 | ||
+ | > aPoint.sumOfSquares() | ||
+ | >>13 | ||
+ | </ | ||
===== Sending messages ===== | ===== Sending messages ===== | ||
- | In AmbientTalk, | + | In AmbientTalk, |
+ | |||
+ | |||
+ | Note that AmbientTalk supports both traditional canonical syntax (e.g. o.m(a,b,c)) as well as keyworded syntax (e.g. o.at: key put: value) for method definitions and message sends. | ||
===== Cloning and instantiation ===== | ===== Cloning and instantiation ===== |
at/tutorial/objects.txt · Last modified: 2013/05/17 20:23 by tvcutsem