smalltalk - Syntax for class and instance variables and methods in Pharo 4.0 -
i learning pharo online , not sure if got syntax correct creating class , instance variables. please correct me if wrong :-
class (static) method created on class side of pharo, name, email, phone instance variables of class createuser:
createnewuser:arguments name:username email:useremail phone:userphone
to call static method of class createuser, following :-
createuser name:username email:useremail phone:userphone
if want create instance variable name, method declaration same above, on instance side of class. however, when call method, call using keyword "new" create new instance under:
createuser new name:username email:useremail phone:userphone
when run above code , call method statically, error message as:-
messagenotunderstood: createuser class >>name:email:phone:
however, when go createuser class recheck, see above method create on class side :
createuser:name:email:phone:
my queries below: 1. doing wrong above? how can fix above error? 2. concept behind using static variables/methods vs class variables/methods same java? 3. if wish access above instance variables, can add accessor methods class/instance , call them class instance/class object instance. correct?
any can give appreciated! in advance.
i guess misunderstand method syntax bit, because createnewuser:arguments
part makes no sense. should have method on class side this:
name: username email: useremail phone: userphone "and here have like:" name := username. email := useremail. "and on"
in example name:email:phone:
method's selector , username
, useremail
, userphone
parameters. can call method in example. name
, email
either class side on instance side variables depending on method defined.
also shouldn't name class createuser
. think this, instances called? "createusers"? name class user, can think instances "users", , responsibility of class object "to create users (its instances)".
please note, it's weird have method on class side. create instance method:
initializename: username email: useremail phone: userphone name := username. email := useremail. phone := userphone
and class side method:
newname: username email: useremail phone: userphone | instance | instance := self new. instance initializename: username email: useremail phone: userphone. ^ instance
or shorter version using cascaded messages:
newname: username email: useremail phone: userphone ^ self new initializename: username email: useremail phone: userphone;
2) in pharo (and smalltalk) concept bit simpler. object, class object also, class side variables , methods instance variables , methods of class instance of "class class". next picture may understand associations between objects in pharo:
this may bit confusing @ beginning, in end, don't have , static/nonstatic methods/variables, have objects, instantiation , inheritance.
so should think going ask objects about. should ask user it's email or mobile number, ask user class create user or find user, or suggest default t-shirt size user.
3) yes, should make accessor. moreover, if select class in system browser , press cmd+h+a (or ctrl, or alt) depending on os, dialog automatic accessor creation
Comments
Post a Comment