calling subclass method does not pass the obj only the argument

4 views (last 30 days)
I have these classes
classdef LUS < handle
methods
function fun1(obj,extravalues)
%some code using extravalues
end
end
end
and:
classdef FLUS < LUS
methods
function fun1(obj,extravalues)
fun1@LUS(obj, extravalues);
%some extra code using extravalues
end
end
end
however when I run
OBJ=FLUS;
OBJ.fun1(extravalues);
then as "fun1" of "LUS" is executed nargin is only 1. Meaning that obj="extravalues" and extravalues is undefined...
How should I call fun1@LUS(obj, extravalues) so that "obj" is passed as "obj" and "extravalues" is passed as "extravalues"?
  4 Comments
Adam
Adam on 13 Dec 2017
Edited: Adam on 13 Dec 2017
Personally, when doing this I favour the option of not over-riding the base class function and instead having a setup as:
classdef LUS < handle
methods
function fun1(obj,extravalues)
% Do some common base class stuff
doFun1( obj, extravalues )
end
end
methods( Access = protected )
function doFun1( obj, extravalues )
end
end
end
and in the subclass:
classdef FLUS < LUS
methods( Access = protected )
function doFun1(obj,extravalues)
%some extra code using extravalues
end
end
end
There are also variations on that that I use, depending if the subclass should always add something (in which case make the function abstract in the super class) or if it should be pre- or post- what the base class does.
It is mostly just a question of taste though. Your version should work from what I see.

Sign in to comment.

Answers (1)

Micke Malmström
Micke Malmström on 25 Apr 2018
This seems to work:
OBJ=FLUS;
OBJ.fun1(OBJ,extravalues);

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!