set and get methods for user-defined hgsetget classes

조회 수: 4 (최근 30일)
Georgios
Georgios 2012년 2월 2일
Dear all,
I would appreciate some feedback regarding the appropriate use of "set" and "get" methods within user-defined classes that inherit from the hgsetget class. My problem has nothing to do with graphics, all I'm trying to do is to program in an OO fashion.
Here's an example. Lets assume there is the user-defined class
classdef myclass < hgsetget
properties (Access = 'protected')
myproperty
end
When I tried to invoke a get method in a different class with something like
value = get(obj,'myproperty');
I got the following error message
"Getting the 'myproperty' property of the 'myclass' class is not allowed."
By inheriting hgsetget I believe I don't have to define the set and get methods if I simply want their primary functionality, but this may be the first point I'm wrong. Please let me know if I must include in the public methods' definition something like
function value = get.myproperty(obj)
value = obj.myproperty;
end
function obj = set.myproperty(obj,value)
obj.myproperty= value;
end
or the problem is elsewhere. Thanks.
PS: in a previous version of my code, I had all my classes inherit a user-defined master class, but this turned out not to be a good thing to do.

답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 2일
Isn't this just because you declared the property to be protected? A protected property should not be accessible outside the methods (or the methods of a subclass.)
  댓글 수: 1
Georgios
Georgios 2012년 2월 2일
Well, the idea is to implement as good data hiding as possible. I believe the properties should be protected, and the class should have public member set and get functions to manipulate the property values outside the class. At least this works in languages such as C++.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Build MATLAB Interface to C++ Library에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by