필터 지우기
필터 지우기

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