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

22 ビュー (過去 30 日間)
Michael Dzjaparidze
Michael Dzjaparidze 2011 年 12 月 17 日
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 Center および File ExchangeConstruct 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