Why do I get an error if do not define a constructor in the inherited class?
이전 댓글 표시
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
per isakson
2013년 1월 24일
It's by design.
Evgeny Pr
2013년 1월 24일
Jim Hokanson
2020년 5월 27일
This seems to be version specific. I'm hopping beween versions right now and 2016b throws the same error but 2019a doesn't ...
채택된 답변
추가 답변 (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
Sean de Wolski
2013년 1월 24일
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
2013년 1월 24일
Evgeny Pr
2013년 1월 24일
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
2013년 1월 24일
Matt J
2013년 1월 24일
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
2013년 1월 24일
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
2013년 1월 25일
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
2013년 1월 25일
All the time? But there will be cases where some parameters are required by the subclass only.
Evgeny Pr
2013년 1월 25일
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
2013년 1월 25일
카테고리
도움말 센터 및 File Exchange에서 Software Development에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!