Smalltalk в примерах/Сообщения: различия между версиями

Содержимое удалено Содержимое добавлено
Строка 67:
 
string := '42' asNumber negated printString.
 
<!--
Methods always return an object (more on this later). This means that you can chain messages together, because there is guaranteed to be an object to send each message to. For example, the following returns -3.
 
3.14 truncated negated
 
When the floating point number receives the truncated message, it returns a SmallInteger, which in turn returns another SmallInteger when sent the negated message. Another example might be a string that contains a number. We want to change the sign on the number and convert it back to a string. One option would be to say:
 
number := '42' asNumber.
negatedNumber := number negated.
string := negatedNumber printString.
 
However, because each method returns an object, we can write this as:
 
string := ( ( '42' asNumber ) negated ) printString.
 
or we can leave out the parentheses since we are dealing only with unary messages, all of which have the same precedence.
 
string := '42' asNumber negated printString.
-->
 
==Приоритет сообщений==