dynamic property subclass use correct get and set methods

1 回表示 (過去 30 日間)
Rajmohan
Rajmohan 2022 年 5 月 18 日
回答済み: Ishan Gupta 2022 年 7 月 27 日
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 Center および File ExchangeClass 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