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

Содержимое удалено Содержимое добавлено
Строка 251:
 
Ключевым моментом, который надо отметить, является то что <i>сам</i> идентифицирует себя самого и может быть просмотрен, так же как переменная, и быть параметром. Однако, <i>супер</i> - это не идентификатор и вы не можете просмотреть его и использовать в качестве параметра. Когда вы выполняете метод, компилятор переводит текст в байткод. Когда он встречает слово <i>супер</i>, он создаёт код который заставляет среду выполнения искать метод в суперклассе класса, определившего метод. Поэтому <i>супер</i> - это просто механизм для сообщения компилятору как создавать байткод.
 
<!--
super
If you remember how message lookup works, Smalltalk looks for the method first in the object that is
receiving the message. If it can't find a method there, it next looks in the superclass, etc. But what do we do if we
explicitly want to start looking in our superclass? Smalltalk provides another special variable, super. So, if you
want to start at your superclass, send a message to super.
When would this be useful? One common example is during instance creation. If you want to initialize some
instance variables you usually write an initialize method on the instance side. You can no longer inherit
new since it doesn't send initialize, so you have to write your own new method, which will inherit the
behavior of new from a superclass. Note that the caret (^) shown below means return.
MyClass>>initialize
... set some variables ...
MyClass class>>new
^super new initialize
In fact, super does not refer the the superclass of the object that received the message. Instead, it refersto the
superclass of the object that defined the code being executed. It's a subtle difference but an important one
because if it were not this way it would be easy to end up in infinite recursion. Let's look at why. Let's say we
have a class hierarchy with ClassTwo subclassed off ClassOne, and ClassThree subclassed off ClassTwo as
shown in Figure 2-2.
All three classes have instance variables that must be initialized and the initialization code looks like the
following.
ClassOne>>initialize
... set some variables ...
ClassTwo>>initialize
super initialize.
... set some variables ...
ClassThree>>initialize
super initialize.
... set some variables ...
When we create an instance of ClassThree and execute the ClassTwo initialize code from the ClassThree
object, what does super refer to? If it refers to the superclass of the class executing the code, then super will be
ClassTwo and the initialize message will be again sent to ClassTwo. Ie, we'll end up in an infinite loop. On
the other hand, if super refers to the superclass of the class defining the code, the message will be sent to
ClassOne and everything works fine.
A key point to note is that self has an identity of its own and can be inspected, assigned to a variable, and
passed as a parameter. However, super has no identity and you cannot inspect it or assign it. When you accept a
method, the compiler compiles the method text into byte codes. When it comes across the word super it
generates byte codes that instruct the run-time engine to start the method lookup in the superclass of the class
defining the method. Thus super is simply a mechanism for telling the compiler how to generate byte codes.
-->
 
[[Smalltalk в примерах/Методы|Методы →]]