A problem in MATLAB OOP documentation
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Geoff Hayes
2015 年 1 月 10 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File 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!