This question already has an answer here:

I'm trying to program using object orientation in Mathematica. For this i need to implement get and set methods. Lets consider that we are working with a person class, that have the fields name and height.So, a instance of the class is: p=person["John",1.74] (person[name,height]) The get methods can be implemented as follow: person/:getName[person[name_,height_]]:=name person/:getHeight[person[name_,height_]]:=height Trying the same approach in the set methods we would have: person/:setName[person[name_,height_],value_]:=name=value person/:setHeight[person[name_,height_],value_]:=height=value But in this, the set method will evaluate the person instance and will be not able to to make the assignment. If, however, we put the attribute HoldFirst in the set methods we will not be able to use the method, because, how the set methods have this attribute, it will not be able to recognize that the entry have the head person. So how can i do this?


marked as duplicate by halirutan, Jens, whuber, István Zachar, Artes Apr 16 '13 at 19:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Duplicate: See this question, its answer and the linked packages for a detailed discussion/implementation of OOP in Mathematica. $\endgroup$ – rm -rf Apr 16 '13 at 17:47

Browse other questions tagged or ask your own question.