【问题标题】:How to call correctly base class constructor from inherited class in Delphi Phrism?如何从 Delphi Phrism 中的继承类中正确调用基类构造函数?
【发布时间】:2011-11-06 07:48:12
【问题描述】:

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

基类:

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/Constructors) 上找到了直接讨论构造函数的网页。

那么,你将如何正确地做到这一点? 谢谢,

【问题讨论】:

    标签: class constructor compiler-errors delphi-prism inherited


    【解决方案1】:

    声明 inherited constructor(aName); 应该这样做。

    【讨论】:

    • @CK,aName 是一个字符串而不是 char 数组,这就是它有问题的原因。我只是将有问题的行更正如下:继承的构造函数(aName.ToCharArray);有用。谢谢,
    • instruction inherited; 单独在子类构造函数开头好像还可以
    • 是的,这也有效。虽然在我回答这个问题时不是
    猜你喜欢
    • 2018-03-31
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多