Number of input parameters in Superclass method

3 次查看(过去 30 天)
I'm a total newbie to OOP and I'm trying to compute a full factorial design depending on the number of input parameter (e.g. speed, distance, time). My superclass does only consider speed, therefore this speed array would be my full factorial design, but in my subclasses i would like to add more parameters (for example distance and time) therefore my design would be a x-by-3 array. I would like to implement the DOE computations only once in the superclass and not in every subclass.
classdef Superclass
properties
speed
params
doe
prop1
end
methods
function obj = Superclass(speed)
obj.speed = speed;
obj.params = {'speed'};
end
function obj = computeDoe(obj)
% Get number of parameters for speed, distance, time and so on
for i = 1:numel(obj.params)
numParams(i) = numel(obj.(obj.params{i}));
end
% Create full factorial design by number of parameters
ff = fullFactorial(numParams);
% Compute DoE
for i = 1:numel(obj.params)
obj.doe(:,i) = obj.(obj.params{i})(ff(:,i));
end
end
end
end
classdef Subclass < Superclass
properties
distance
time
prop2
end
methods
function obj = Subclass(speed, distance, time)
obj@Superclass(speed)
obj.distance = distance;
obj.time = time;
obj.params = {'speed', 'distance', 'time'};
end
end
end
superClassObj = Superclass([1 2 3])
superClassObj.computeDoe;
superClassObj.doe
subClassObj = Subclass([1 2 3], 4:7, [0.1 0.2])
subClassObj = subClassObj.computeDoe
subClassObj.doe
I need to know in my computeDoe method how many parameters I have for creating my doe design, how can i do that without saving the involved parameters on my obj.params property? My code works but i doubt that this is a good solution...i don't really know which kind of OOP mechanism to apply on this right now. If i use for example an Abstract Class, i have to implement the computeDoe method in every subclass or am I completely wrong? Thanks a lot for your help!

采纳的回答

Matt J
Matt J 2023-9-7
编辑:Matt J 2023-9-7
My design would be as below.
classdef Superclass
properties
params %now a struct
doe
prop1
end
methods
function obj = Superclass(speed)
obj.params.speed = speed;
end
function obj = computeDoe(obj)
% Get number of parameters for speed, distance, time and so on
numParams=structfun(@numel,obj.params);
paramNames=fieldnames(obj.params);
% Create full factorial design by number of parameters
ff = fullFactorial(numParams);
% Compute DoE
for i = 1:numel(paramNames)
obj.doe(:,i) = obj.(obj.params.(paramsNames{i}) )(ff(:,i));
end
end
end
end
classdef Subclass < Superclass
properties
prop2
end
methods
function obj = Subclass(speed, distance, time)
obj@Superclass(speed)
obj.params.distance=distance;
obj.params.time=time;
end
end
end
  2 个评论
Matt J
Matt J 2023-9-7
编辑:Matt J 2023-9-7
If you don't want to go through the params member to access your parameters, you can use Dependent properties or overloaded subsref/subsasgn methods to define shorthands, e.g.,
classdef Superclass
properties
params %now a struct
doe
prop1
end
methods
function obj = Superclass(speed)
obj.params.speed = speed;
end
function obj = computeDoe(obj)
% Get number of parameters for speed, distance, time and so on
....
end
function val=subsref(obj,S)
if S(1).type=="." && ismember(S(1).subs, fieldnames(obj.params))
val=builtin('subsref',obj.params,S);
else
val= builtin('subsref',obj,S);
end
end
end
end
Jens Gaechter
Jens Gaechter 2023-9-8
Yes using a struct for saving my involved parameters instead of using a separate property makes completely sense. I will also check out your suggestion via subsref - thanks a lot Matt!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by