Clear Filters
Clear Filters

A problem in MATLAB OOP documentation

1 view (last 30 days)
Jack
Jack on 10 Jan 2015
Commented: Jack on 11 Jan 2015
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.

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Jan 2015
Jack - I think that the purpose of this attribute (if set to true) is to allow handle subclasses derived from Utility to be compatible with that base class. In the above example, the derived class PropertyDefaults is a Value class (like its parent Utility), so removing the HandleCompatible would have no effect.
Suppose then that we derive another class (based on the examples from the Supporting Both Handle and Value Subclasses) that is a handle class
classdef PropertyDefaultsHandle < Utility & handle
properties
p1 = datestr(rem(now,1)); % Some time
p2 = 'blue'; % Character string
p3 = 42.4242/2; % Result of division operation
end
end
Now, if you instantiate the class as
pd = PropertyDefaultsHandle
and follow through with
pd.p1 = datestr(rem(now,1));
pd.p2 = 'green';
pd.p3 = pi/4;
you can reset to the defaults simply (without the assignment seen in the previous example) as
pd.resetDefaults
and pd is back to its default self. This example demonstrates how the attribute (when set to true) can allow handle and value subclasses to be derived from the same base value class.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!