OOP: object setting property in a method without overriding object in workspace

Let's say I have class pizza with properties pepperoni. Then i have method combo1 that sets pepperoni = 1;
Ok that was easy, but now in the workspace I type...
pizza1=pizza; %creates object
pizza1.combo1;
so now pepperoni should be equal to 1....
nope, pizza1.pepperoni is not 1.
how do I do this without resaving the object to pizza1?
ex pizza1=pizza1.combo1; pizza1.pepperoni1=;

 Accepted Answer

Derive from the handle class.

4 Comments

From what I saw, Richard does't seem to want t have pizza1 and pizza points to the same object. I could be wrong though and in that case handle class is indeed a solution. However, what Richard had should work even if it's a value class, so that bothers me the most.
For a value class, an object with the pepperoni property set to 1 would be returned by pizza1.combo1, but that return value would have to be assigned to pizza1 if pizza1 is to be updated.
With class pizza being derived from handle class, pizza1.combo1 would be modifying the instantiated object, whose handle designated by pizza1 would stay the same.
pizza1=pizza; would not result in having the two point to the same object if pizza is the name of the class constructor. Unless, that is, one needed to use pizza1=pizza() in order to force interpretation as a constructor rather than a class name, but I do not remember seeing that as ever being necessary.
Oh I see, I misread Richard's post, I didn't realize pizza is the class constructor, I thought it's an incidence.
thanks guys, ill try it out on my pizza tomorrow.

Sign in to comment.

More Answers (1)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!