Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
complexvalues [2019/01/04 16:21] christian [Using Values] |
complexvalues [2019/01/04 16:50] christian [Anatomy of a Value] |
||
---|---|---|---|
Line 38: | Line 38: | ||
</ | </ | ||
- | And you can do: | + | Withit |
* create a value with <code smalltalk> | * create a value with <code smalltalk> | ||
- | * ask it for its parts <code smalltalk> | + | * ask for its parts <code smalltalk> |
- | * print the instance | + | * print it as code <code smalltalk> |
' | ' | ||
name: '' | name: '' | ||
Line 47: | Line 47: | ||
birthday: (Date d: 25 m: 6 y: 1960)' "</ | birthday: (Date d: 25 m: 6 y: 1960)' "</ | ||
* get the value from its code <code smalltalk> | * get the value from its code <code smalltalk> | ||
+ | * add fancy access methods like <code smalltalk> | ||
+ | weekdayAtBirth | ||
+ | ^self birthday weekday | ||
+ | </ | ||
Line 76: | Line 80: | ||
The variables are set all at once by an initalizing method which has all initial values as parameters. | The variables are set all at once by an initalizing method which has all initial values as parameters. | ||
The object becomes immutable after initialization and all instance variables are effectively constants. | The object becomes immutable after initialization and all instance variables are effectively constants. | ||
+ | |||
+ | The parameter names are the concatenated name and its class. This prevents name clashes, is systematic and still readable. | ||
<code smalltalk> | <code smalltalk> | ||
- | Person>> | + | Person>> |
- | name := aString. | + | name := nameString. |
- | sex := aSymbol. | + | sex := sexSymbol. |
- | birthday := aDate. | + | birthday := birthdayDate. |
self beImmutable | self beImmutable | ||
</ | </ | ||
Line 88: | Line 94: | ||
<code smalltalk> | <code smalltalk> | ||
- | Person class>> | + | Person class>> |
| inst | | | inst | | ||
inst := self new. | inst := self new. | ||
- | inst initializeName: | + | inst initializeName: |
^inst | ^inst | ||
</ | </ | ||
Line 111: | Line 117: | ||
< | < | ||
< | < | ||
- | < | + | < |
</ | </ | ||
=== Defaults === | === Defaults === | ||
- | The simple values above are not very interesting. But when you define defaults for some of the variables, Values become more useful | + | The simple values above are not very interesting. But when you define defaults for some of the variables, Values become more useful. Lets add a nickname to the specification: |
+ | |||
+ | <code smalltalk> | ||
+ | Person class>> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | After generating code with: <code smalltalk> | ||
+ | |||
+ | <code smalltalk> | ||
+ | Person example nickname | ||
+ | </ | ||
+ | |||
+ | and there is a new constructor available: | ||
+ | <code smalltalk> | ||
+ | Person class>> | ||
+ | </ | ||
==== Get it ==== | ==== Get it ==== |