Correct way of implementing a getter and setter for a handle class

17 vues (au cours des 30 derniers jours)
Michael Dzjaparidze
Michael Dzjaparidze le 17 Déc 2011
Is it syntactically correct to implement getter and setter methods for a MATLAB handle class the same way as you would do for a value class? I.e. by implementing a
function obj = set.testVar(obj,newVal)
funtion value = get.testVar(obj)
Or is it necessary to always inherit from hgsetget if you want getter/setter functionality for any handle class. The reason I ask is because the above does seem to work, although I do get a "Probable conflict between handle and value class usage", a "The class ClassName is derived from a 'handle' class, so must be a handle class warning" and a "Method set.testVar has a signature consistent with a value class" warning.

Réponse acceptée

Daniel Shub
Daniel Shub le 17 Déc 2011
For handle classes it should be
function set.testVar(obj,newVal)
You do not need to return the obj, since you already have the object. For value classes you need to return the object, because the setter in essence creates a new object.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by