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

12 次查看(过去 30 天)
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.

采纳的回答

Daniel Shub
Daniel Shub 2011-12-17
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by