简体   繁体   English

pharo smalltalk中的调用方法

[英]Calling methods in pharo smalltalk

I'm trying to call a function from another class (Binario) but it says it's not implemented. 我正在尝试从另一个类(Binario)调用一个函数,但是它说它没有实现。

This is the code for the method in Binario class: 这是Binario类中方法的代码:

  genlista
  ^ (1 to: 30) collect: [ :i | 2 atRandom - 1 ]

And this is the code for the other class method: 这是另一个类方法的代码:

 ListadelistasBin
  | bin |
  bin := Binario new.
  ^ (1 to: 30) collect: [ :i | bin genlista ]

Please, help me :( 请帮我 :(

Most likely @Uko is right and you defined the method in the class side of Binario rather than in the instance side. @Uko很可能是正确的,您在Binario的类侧而不是在实例侧定义了方法。 One way to check this would be to modify your second method like this: 一种检查方法是修改您的第二种方法,如下所示:

ListadelistasBin
  | bin |
  bin := Binario.                            "<- new removed"
  ^ (1 to: 30) collect: [:i | bin genlista]

If now you get the answer, then what happened is that your genlista method is in the wrong place (class side instead of instance side). 如果现在您得到了答案,那么发生的是您的genlista方法放在错误的位置(类方而不是实例方)。

In Smalltalk every method belongs in a class. 在Smalltalk中,每个方法都属于一个类。 However, there are two "sides" of a class. 但是,一个类有两个“方面”。 The instance side is where you put methods for the instances of the class. 实例侧是放置类实例的方法的地方。 The class side is where you put methods for the class itself. 方面,您可以为类本身放置方法。

How can you tell in what side of a class you have saved a method? 您如何知道在类的哪一侧保存了方法? Just look for the switch that every browser has to select one or the other side. 只需寻找每个浏览器都必须选择一侧或另一侧的开关即可。 In Pharo, for example, there is a toggle button that you use to select each of the sides. 例如,在Pharo中,有一个切换按钮可用于选择每侧。

While instance methods define the behavior of the instance of your class (and subclasses), class methods are meant to be sent to the class. 实例方法定义了类(和子类)实例的行为,而类方法是要发送给类的。 This is just a consequence of classes being objects. 这只是类是对象的结果。 For example, Binario new is a message sent to the class Binario , and we believe that your intention was to define the genlista method for instances of Binario . 例如, Binario new被送到类的消息Binario ,我们相信,你的目的是为了定义genlista的实例方法Binario If this is the case, then copy the source code of the method and paste it on the instance side of the class. 如果是这种情况,则复制该方法的源代码并将其粘贴到类的实例侧。 Then remove the class method and try again. 然后删除类方法,然后重试。 Ah! 啊! and don't forget to put the new message back next to Binario in your ListadelistasBin ! 不要忘记把new消息发回旁边BinarioListadelistasBin

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

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