100问一简单问题(OO) 在delphi中在子类构造方法中怎么调用父类的构造方法。有没有类似CALL ,SUPPER之类的关健字可用? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 继承的时候,覆盖构造方法在子类的构造方法中加inherted不知道这样行不? TParent = class(TObject) constructor Create; end; TChild = class(TParent) constructor Create; end;implementation{ TParent }constructor TParent.Create;begin ShowMessage('TParent 创建对象')end;{ TChild }constructor TChild.Create;begin inherited; Showmessage('TChild 创建对象')end; 还是不行,我的父类和子类的构造方法全是有参数的(如下)。而且参数个数还不一样的。怎么办?----------------------------------------------------------------------------------------TParent = class(TObject) constructor Create(参数一,参数二,参数三);//****************************** end; TChild = class(TParent) constructor Create(参数一,参数二,参数三,参数四);//****************************** end;implementation{ TParent }constructor TParent.Create;begin ShowMessage('TParent 创建对象')end;{ TChild }constructor TChild.Create;begin inherited; Showmessage('TChild 创建对象')end; type IMoveTo = interface(IUnKnown) procedure Move(Target: string); end; TParent = class(TObject) constructor Create(a: string); end; TChild = class(TParent) constructor Create(a: string; b: string); end;implementationconstructor TParent.Create(a: string);begin ShowMessage('P: ' + a);end;constructor TChild.Create(a: string; b: string);begin Inherited Create(a); ShowMessage('C: ' + b);end;end. delphi在子类调用父类方法上做得非常好,一个inherited语句就OK了,不过要记住的是子类的构造函数一定要记得加上inherited,因为delphi的类构造函数是不会自动去调用父类的构造函数。 直接用inherited,不过我觉得这句其实就是等于父类同名方法里的整个代码 如果子类要重载父类的构造函数,而父类是虚构造函数的话,要加上Reintroduce。 如何调用XP系统自带的屏幕键盘 Raize 哪个控件可做成OutLookBar TRDSConnection组件是干什么的?? 100 请问:关于TRichEdit DELPHI编写时的快捷键,高分! 有谁知道这个错误是什么意思? CSDN商城支付配送开始多样化?什么意思是共享软件销售的支付方式吗? 多窗口浏览器换肤问题求教 dana来领分;150要3贴真倒霉;唉,csdn.... ADOCommand如何批量执行SQL语句? 使用openpicturedialoge的奇怪问题
在子类的构造方法中加inherted
不知道这样行不?
constructor Create;
end; TChild = class(TParent)
constructor Create;
end;
implementation{ TParent }constructor TParent.Create;
begin
ShowMessage('TParent 创建对象')
end;{ TChild }constructor TChild.Create;
begin
inherited;
Showmessage('TChild 创建对象')
end;
----------------------------------------------------------------------------------------
TParent = class(TObject)
constructor Create(参数一,参数二,参数三);//******************************
end; TChild = class(TParent)
constructor Create(参数一,参数二,参数三,参数四);//******************************
end;
implementation{ TParent }constructor TParent.Create;
begin
ShowMessage('TParent 创建对象')
end;{ TChild }constructor TChild.Create;
begin
inherited;
Showmessage('TChild 创建对象')
end;
IMoveTo = interface(IUnKnown)
procedure Move(Target: string);
end; TParent = class(TObject)
constructor Create(a: string);
end; TChild = class(TParent)
constructor Create(a: string; b: string);
end;implementationconstructor TParent.Create(a: string);
begin
ShowMessage('P: ' + a);
end;constructor TChild.Create(a: string; b: string);
begin
Inherited Create(a);
ShowMessage('C: ' + b);
end;end.
不过要记住的是子类的构造函数一定要记得加上inherited,因为delphi的类构造函数是不会自动去调用父类的构造函数。