简体   繁体   English

发送给Smalltalk中超类的“class”消息

[英]“class” message sent to superclass in Smalltalk

Let us consider following classes: 让我们考虑以下课程:

Object subclass: Sup [].

Sup subclass: Sub [ print_superclass [ super class printOn: stdout. ] ].

When I try to run print_superclass method on Sub I get 当我尝试在Sub上运行print_superclass方法时得到

> Sub new print_superclass.
Sub

I expected here to get Sup because the class call was moved back to the superclass of Sub which is Sup . 我期望在这里获得Sup因为class调用被移回Sub的超类,即Sup Why does it behave like this? 为什么它会像这样?

Because super is a pseudo-variable pointing to the receiver of the message. 因为super是指向消息接收者的伪变量。 Super and self point to the same object and have the same identity. 超级和自我指向同一个对象并具有相同的身份。

super == self ---> true

The difference between them is that super tells the message lookup to start searching the next in the method dictionary "above" that containing the method. 它们之间的区别在于super告诉消息查找开始在包含该方法的“上面”方法字典中搜索下一个。

The definition is confusing, but in this case, super only says that the method search for #class does not start in Sub methods, but in Sup methods. 该定义令人困惑,但在这种情况下,super仅表示搜索#class的方法不是在Sub方法中启动,而是在Sup方法中启动。 However, it does not have effect because #class is defined in a higher level of the hierarchy and its implementation refers to the class of the receiver, that is an instance of Sub 但是,它没有效果,因为#class是在层次结构的更高级别定义的,它的实现是指接收器的类,即Sub的一个实例

The behavior you get is the expected one. 你得到的行为是预期的行为。 The key is in the semantics of super . 关键在于super的语义。 Let's see some examples before analyzing your case: 在分析您的案例之前,让我们看一些例子:

Example 1 例1

ClassA         "implements msg"
  ClassB       "implements msg"
    ClassC     "implements msg"

This means that the inherited version of msg is overridden in ClassB and ClassC . 这意味着在ClassBClassC重写了继承的msg版本。 In this case 在这种情况下

super msg       "sent from ClassC invokes ClassB >> msg"
super msg       "sent from ClassB invokes ClassA >> msg"
super msg       "sent from ClassA will signal MessageNotUnderstood"

(I'm assuming msg is not implemented above ClassA ) (我假设msg没有在ClassA之上实现)

Example 2 例2

ClassA         "implements msg"
  ClassB       "does not implement msg"
    ClassC     "implements msg"

Then 然后

super msg       "sent from ClassC invokes ClassA >> msg"
super msg       "sent from ClassB invokes ClassA >> msg"

Example 3 例3

ClassA         "implements msg"
  ClassB       "does not implement msg"
    ClassC     "does not implement msg"

Here 这里

super msg       "sent from ClassC invokes ClassA >> msg"
super msg       "sent from ClassB invokes ClassA >> msg"

So, the semantics of super is: start the lookup in my superclass . 所以, super的语义是: 在我的超类中启动查找

Your case 你的情况

Yo have 哟有

Object         "implements class"
  Sup          "does not implement class"
    Sub        "does not implement class"

Therefore, when you send super class from Sub it will invoke Object >> class , right? 因此,当你从Sub发送super class ,它会调用Object >> class ,对吧? Which is the same as sending self class (because class is not implemented in Sub ), which is Sub . 这与发送self class相同(因为class未在Sub实现),即Sub And since Sub new print_superclass sends super class from Sub , you get Sub . 而且由于Sub new print_superclassSub发送super class ,你得到Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM