A problem in MATLAB OOP documentation
이전 댓글 표시
In MATLAB R2014b OOP documentation in page '10-33' we have some codes about 'Supporting Both Handle and Value Subclasses'. You can see the codes here:
classdef (HandleCompatible) Utility
methods
function obj = resetDefaults(obj)
% Reset properties to default and return object
mc = metaclass(obj); % Get meta.class object
mp = mc.PropertyList; % Get meta.property objects
for k=1:length(mp)
% For each property, if there is a default defined,
% set the property to that value
if mp(k).HasDefault && ~strcmp(mp(k).SetAccess,'private')
obj.(mp(k).Name) = mp(k).DefaultValue;
end
end
end
end
end
classdef PropertyDefaults < Utility
properties
p1 = datestr(rem(now,1)); % Current time
p2 = 'red'; % Character string
p3 = pi/2; % Result of division operation
end
end
Now we insert this :
pd = PropertyDefaults with properties:
p1: ' 4:54 PM'
p2: 'red'
p3: 1.5708
Assign new values that are different from the default values:
pd.p1 = datestr(rem(now,1));
pd.p2 = 'green';
pd.p3 = pi/4;
All pd object property values now contain values that are different from the default values originally defined by the class:
pd =
PropertyDefaults with properties:
p1: ' 5:36 PM'
p2: 'green'
p3: 0.7854
Call the resetDefaults method, which is inherited from the Utility class. Because the PropertyDefaults class is not a handle class, you must return the modified object for reassignment in the calling function's workspace.
pd = pd.resetDefaults
pd =
PropertyDefaults with properties:
p1: ' 4:54 PM'
p2: 'red'
p3: 1.5708
This part of Object-oriented programming documentation is about using 'HandleCompatible' class attribute but when we remove this attribute from Utility class we have same behavior!! what is purpose of this attribute? I'm using MATLAB R2014b.
Thanks.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!