在父级之前初始化子级-不要调用父级构造函数 [英] Initialize child before parent - do not call parent contructor
问题描述
在MatLab中,我有一个超类A,它带有一些参数x和y
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
应该使用initialize方法进行一些初始计算,以便稍后进行计算.
The initialize method is supposed to do some initial calculations to fasten computation later.
现在,我想创建A的子类B,它具有自己的初始化,但是B应该在A之前初始化.
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 = this@A([], []);
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
基本上,我只想在A之前初始化B,但是由于必须首先调用父级的构造函数,所以我不知道该怎么做.
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.
在PHP中,我会做类似的事情
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
}
}
是否有一种聪明的方法来设计MatLab中我想要的东西?我必须放弃B类中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?
推荐答案
这违反了OOP的原理.您基本上想要实现的目标排除了B
应该从A
派生的情况.据我所知,您确实想重载A
的构造函数.可以使用nargin
签入A
来执行此操作,或者如果您出于某种原因确实想要B
,则只需在B
的构造函数中使用:this = this@A(p(1), p(2);
即可正确地调用B
的构造函数A
(并删除属性p
).
This goes against the principles of OOP. What you are trying to achieve basically rules out that B
should be derived from A
. From what I can tell, you really want to overload the constructor of A
. Either do this using a nargin
check in A
, or if you really want to have a B
for whatever reason, just use: this = this@A(p(1), p(2);
in the constructor of B
instead, to properly call the constructor of A
(and get rid of the property p
).
这是您使用nargin
模拟到重载构造函数的方式:
This is how you would simulate to overload the constructor by using nargin
:
classdef A < handle
properties
x;
y;
z;
end
methods
function this = A(x, y)
if nargin==1 % 'p' was passed as 'x'
this.x = x(1);
this.y = x(2);
else
this.x = x;
this.y = y;
end
this.initialize();
end
function initialize(this)
this.z = this.x + this.y;
end
end
end
这是从A
派生B
的方法,并正确调用了A
的构造函数:
This is how you would derive B
from A
, with a proper call of A
's constructor:
classdef B < A
methods
function this = B(p)
this = this@A(p(1), p(2));
end
end
end
这篇关于在父级之前初始化子级-不要调用父级构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!