dynamic property subclass use correct get and set methods

2 次查看(过去 30 天)
The superclass definition is -
classdef calibration ... % Class name
< dynamicprops %% Superclass
% dynamicprops makes handle class
% Creation Properties
properties (SetAccess = immutable)
created = datestr(now, 'yyyymmddTHHMMSS');
author = getenv('username');
end
% Properties
properties
name = '';
end
% Dynamic Property
properties (SetAccess = protected, Hidden = true)
metaDynProp = [];
end
methods
val = get(obj)
set(obj)
end
% Methods
methods
function addCal(obj,calName,calVal)
% ADDCAL adds calibration to a cal object
% Determine calibration type
if isscalar(calVal)
caseNum = 1;
elseif isstruct(calVal) && length(calVal) == 4
caseNum = 2;
elseif isstruct(calVal) && length(calVal) == 6
caseNum = 3;
else
error('Invalid Calibration')
end
% Create Calibration
obj.metaDynProp.(calName) = obj.addprop(calName);
%obj.metaDynProp.(calName).SetAccess = 'private';
switch(caseNum)
case 1 % Scalar case
% Assign Methods
obj.(calName) = scalar;
%obj.metaDynProp.(calName).SetMethod = 'set@scalar';
%obj.metaDynProp.(calName).GetMethod = @scalar.get;
case 2 % Table case
% Assign Methods
case 3 % Map case
% Assign Methods
end
% Set Property
set(obj.(calName),calVal);
end
end
end
The subclass definition is
classdef scalar < calibration
properties
value = [];
end
% Methods
methods
function set(obj,value)
obj.value = value;
end
function value = get(obj)
value = obj.value;
end
end
end
I would like to have the functionality where a calibration can be added such as
x = calibration;
x.addCal('a',1);
Then when x.a is called, the output is 1. However, with the above implementation the output is a scalar object. Also, is there a way to prevent properties from being inherited?
  1 个评论
Seth Furman
Seth Furman 2022-5-24
Note that, for datetime formats
  • M corresponds to month
  • m corresponds to minute
  • s corresponds to second
  • S corresponds to fractional second
so
created = datestr(now, "yyyymmddTHHMMSS");
as a datetime would be
created = datetime("now", Format="yyyyMMdd'T'HHmmss")
created = datetime
20220524T174204

请先登录,再进行评论。

回答(1 个)

Ishan Gupta
Ishan Gupta 2022-7-27
You can set property as Private to avoid getting inherited.
Please refer this.

类别

Help CenterFile Exchange 中查找有关 Class Introspection and Metadata 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by