创建从另一个类继承的类时,当创建派生的类时,基本类的构造函数被称为?
Type
TBase = Class
constructor xMain;
End;
TDerived = Class(TBase)
constructor xMain;
End;
constructor TBase.xMain;
begin
MessageBox(0,'TBase','TBase',0);
end;
constructor TDerived.xMain;
begin
MessageBox(0,'TDerived','TDerived',0);
end;
Var
xTClass:TDerived;
begin
xTClass := TDerived.xMain;
end.
我认为这应该导致一个消息框显示" TBASE",然后显示" Tdedived"。但是,事实并非如此。上面的代码运行时,仅导致一个消息框显示" tdedived"。
constructor TDerived.xMain;
begin
inherited;
MessageBox(0,'TDerived','TDerived',0);
end;
在tderived.xmain中添加继承;否则,祖先的代码将不会被调用;
begin
inherited;//call the ancestor TBase.xMain
MessageBox(0,'TDerived','TDerived',0);
end;
此问题还可以帮助您理解继承的保留单词:
delphi:如何在虚拟方法上调用继承的祖先?
另一个不错的资源是http://www.delphibasics.co.uk/rtl.asp?name = inherited