In MatLab, i have a superclass A that takes some parameters, x and y
classdef A < handle
properties
x;
y;
z;
end
methods
function this = A(x, y)
this.x = x;
this.y = y;
this.initialize();
end
function initialize(this)
this.z = this.x + this.y;
end
end
end
The initialize method is supposed to do some initial calculations to fasten computation later.
Now i want to create a child class B of A, with it's own inialization, but B should be initialized before A.
classdef B < A
properties
p
end
methods
% B takes another parameter p
function this = B(p)
% Since B is a subclass of A, and A takes some parameters we have to
% call the constructer of A as the first thing.
this = [email protected]([], []);
this.p = p;
end
function initialize(this)
this.x = this.p(1);
this.y = this.p(2);
% This is when initialize of A should be called.
end
end
end
Basically i just want to initialize B before A, but because the constructer of the parent have to be called as the first thing, i cannot figure out how to do this.
In PHP I would do something like this
class A {
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
$this->initialize();
}
public function initialize() {
$this->z = $this->x + $this->y;
}
}
class B extends A {
public function __construct($p) {
// Does not call the parent constructor.
$this->p = $p;
$this->initialize();
}
public function initialize() {
$this->x = $this->p[0];
$this->y = $this->p[1];
parent::initialize(); // Now we are ready to initialize A
}
}
Is there a smart way to design what i want in MatLab? Do i have to give up the inheritance of A in class B?
The complication arises since:
If you create a subclass object, MATLABĀ® calls the superclass constructor to initialize the superclass part of the subclass object. By default, MATLAB calls the superclass constructor without arguments. [src]
You can get around explicitly calling the superclass constructor by wrapping its work in an if
-statement dependent on how many arguments it was passed (nargin
):
function this = A(x, y)
if (nargin ~= 0) % or (nargin == 2) in this case
this.x = x;
this.y = y;
this.initialize();
end
end
And then B
's methods would look like:
function this = B(p)
this.p = p;
this.initialize();
end
function initialize(this)
this.x = this.p(1);
this.y = this.p(2);
[email protected](this);
end
It may just be error in the post, but A
's initialize
method is referencing undefined variables x
and y
and not their properties. It should be:
function initialize(this)
this.z = this.x + this.y;
end