简体   繁体   中英

Calling methods in pharo smalltalk

I'm trying to call a function from another class (Binario) but it says it's not implemented.

This is the code for the method in Binario class:

  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. 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).

In Smalltalk every method belongs in a class. 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.

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 . 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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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