加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:13:07 所属栏目:大数据 来源:网络整理
导读:我有两个类 – 基类和继承类如下. 基类: TAlarm = class(System.Object)private:protected:public: constructor (tag:TTagname);end; 继承类: TAlarmMsg = class(TAlarm) public constructor (aname:string); method GetAlarmMsg:string; override; method
我有两个类 – 基类和继承类如下.

基类:

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.和/或
– 在基类中找不到合适的构造函数,因此需要手动调用inherited

顺便说一下,我花了半天时间研究这个问题,并在网上找到了很好的信息.到目前为止没有任何帮助.我甚至找到了直接在Delphi Prism Wikipedia(http://prismwiki.embarcadero.com/en/Constructors)上讨论构造函数的网页.

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

解决方法

继承的构造函数(aName)应该这样做.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读