필터 지우기
필터 지우기

dynamic property subclass use correct get and set methods

조회 수: 2 (최근 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 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