Why do I get an error if do not define a constructor in the inherited class?
Mostrar comentarios más antiguos
Here's a simple example that does not work in the MATLAB language:
classdef Bar < handle
% Bar class
properties
Value
end
methods
function self = Bar(value)
self.Value = value;
end
end
end
classdef Foo < Bar
% Foo class
% No has constructor. We want to use the "Bar" constructor
end
>> f = Foo(10)
Error using Foo
Too many input arguments.
Too many input arguments.???
Ok...
>> f = Foo()
Error using Bar (line 10)
Not enough input arguments.
Error in Foo (line 1)
classdef Foo < Bar
What's going on? This is the ordinary inheritance. I would not want every time to write this:
classdef Foo < Bar
methods
function self = Foo(value)
self = self@Bar(value);
end
end
end
3 comentarios
per isakson
el 24 de En. de 2013
It's by design.
Evgeny Pr
el 24 de En. de 2013
Jim Hokanson
el 27 de Mayo de 2020
This seems to be version specific. I'm hopping beween versions right now and 2016b throws the same error but 2019a doesn't ...
Respuesta aceptada
Más respuestas (1)
Write the Bar constructor to handle the case when no input arguments are passed. Otherwise, it needs to get those arguments from somewhere, e.g.,
function self = Bar(varargin)
if nargin
value=varargin{1};
else
value=somedefault;
end
self.Value = value;
end
17 comentarios
Sean de Wolski
el 24 de En. de 2013
classdef Bar < handle
% Bar class
properties
Value
end
methods
function self = Bar(value)
if nargin
self.Value = value;
else
self.Value = pi;
end
end
end
end
Seems to work
Evgeny Pr
el 24 de En. de 2013
Evgeny Pr
el 24 de En. de 2013
It's strange that I can't do this in matlab. Do not define a constructor each time:
You don't have to define a base class constructor in MATLAB. It can always default to an internal constructor. Even if you do define an explicit base class constructor, you do not have to pass arguments to it from the subclass constructor as long as the base constructor can handle the no-argument case. From the OOP doc:
if the class being created is a subclass, MATLAB calls the constructor of each superclass class to initialize the object. Implicit calls to the superclass constructor are made with no arguments. If superclass constructors require arguments, you must call them from the subclass constructor explicitly.
Evgeny Pr
el 24 de En. de 2013
Matt J
el 24 de En. de 2013
What limitation are you talking about? The message I've been trying to convey to you is that you are not required to do anything.
Matt J
el 24 de En. de 2013
I'm not sure why you think you have to. Below is an example of a base class (myclass) which defines a constructor, but the subclass (mysubclass) does not. I can construct an instance of the subclass no problem
>> obj=mysubclass
obj =
mysubclass
Properties:
value: 0
Methods, Superclasses
###################
classdef myclass
properties
value;
end
methods
function obj=myclass(value)
if nargin
v=value;
else
v=0;
end
obj.value=v;
end
end
end
###############
classdef mysubclass < myclass
end
I'm not familiar enough with Python to understand your example fully, but in any OOP design, I think, you need some way for a subclass constructor to distinguish between which of its constructor parameters go to the base class part of the object and which of its parameters are needed by the subclass part.
Apparently, the 'pass' command you issue is a way of saying "send all parameters to the base class"? If so, it seems like it plays an equivalent role to
self = self@BaseClass(...);
and I don't really see the big difference.
Matt J
el 25 de En. de 2013
Well, then you're saying MATLAB should assume all parameters are to be fed to the base constructor unless told differently? That might make sense if you believe the subclass and base class will often require identical parameters. I don't know if that's likely for everybody...
Matt J
el 25 de En. de 2013
All the time? But there will be cases where some parameters are required by the subclass only.
Evgeny Pr
el 25 de En. de 2013
Then you should correct what you said here
I want matlab transfers to the constructor of the superclass all parameters, unless the subclass constructor is not define.
You really meant to say "I want matlab to transfer to the constructor of the superclass all parameters, IF the subclass constructor is not defined."
Yes, I think I can see what you mean. Although, it somehow seems cleaner to me that for every function call
subclassConstructor(params)
there is a function signature to match. But that might just be the bias of my experience...
Evgeny Pr
el 25 de En. de 2013
Categorías
Más información sobre Construct and Work with Object Arrays en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!