In smalltalk, when we create a object by calling new which calls initialize . I want to initialize but with my own parameters(passed at run time). How can I do that.
e.g. Myobjcet new
but how do I pass parameters to this so they get passed to initialize. I am using Pharo.
If I recall, reimplementing class methods new
and initialize
should be avoided.
Instead, you can create your own class method (other than new or initialize) that takes parameters, and use those when creating your new instance. For example in Squeak
look at the class method with:
for class Collection
. It first creates a collection instance, and then adds to the instance the object passed as an argument.
with: anObject
"Answer an instance of me containing anObject."
^ self new
add: anObject;
yourself
Your Pharo
maybe based on Squeak, so you should find the same, or a similar class method for Collection
in your image.