如何在Delphi Phrism中从继承类正确调用基类构造函数?

发布于 2024-12-01 21:15:08 字数 1027 浏览 1 评论 0原文

我有两个类 - 基类和继承类,如下所示。

基类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-12-08 21:15:08

声明
继承的构造函数(aName);
应该这样做。

The statement
inherited constructor(aName);
should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文