如何在Delphi Phrism中从继承类正确调用基类构造函数?
我有两个类 - 基类和继承类,如下所示。
基类:
TAlarm = class(System.Object)
private:
protected:
public:
constructor (tag:TTagname);
end;
继承类:
TAlarmMsg = class(TAlarm)
public
constructor (aname:string);
method GetAlarmMsg:string; override;
method SendMsg(msg:string);
end;
构造函数:
constructor TAlarm(tag:TTagname);
begin
Tagname := tag;
end;
constructor TAlarmMsg(aname:string);
begin
inherited TAlarm(aname); <========Here is my problem.
name := aname.ToCharArray;
end;
无论我如何调用或使用继承的构造函数,我都会不断收到以下错误消息当我编译源文件时。
- 在继承的构造函数完成之前无法访问 Self。和/或 - 在基类中找不到合适的构造函数,因此需要手动调用继承
顺便说一下,我花了半天时间研究这个问题,并在网上找到了很好的信息。到目前为止没有任何帮助。我什至在 Delphi Prism Wikipedia 上找到了直接谈论构造函数的网页( http://prismwiki.embarcadero.com/ en/构造函数)。
那么,你会如何正确地做到这一点呢? 谢谢,
I have two classes - base class and inherited class as follows.
Base Class:
TAlarm = class(System.Object)
private:
protected:
public:
constructor (tag:TTagname);
end;
inherited class:
TAlarmMsg = class(TAlarm)
public
constructor (aname:string);
method GetAlarmMsg:string; override;
method SendMsg(msg:string);
end;
constructors:
constructor TAlarm(tag:TTagname);
begin
Tagname := tag;
end;
constructor TAlarmMsg(aname:string);
begin
inherited TAlarm(aname); <========Here is my problem.
name := aname.ToCharArray;
end;
No matter what or how I call or play around with inherited constructor, I keep getting the following error messages when I compile the source file.
- Self cannot be accessed before the inherited constructor has finished. And/OR
- Cannot find appropriate constructor in base class so manual call to inherited is required
By the way, I have spent good half a day researching on this issue and have found good information online. Nothing helps so far. I even found the webpage that directly talks about constructors on Delphi Prism Wikipedia ( http://prismwiki.embarcadero.com/en/Constructors ).
So, how would you do it correctly?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
声明
继承的构造函数(aName);
应该这样做。
The statement
inherited constructor(aName);
should do it.