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

delphi重载,覆盖,虚方法

发布时间:2020-12-15 09:46:14 所属栏目:大数据 来源:网络整理
导读:有如下的简单对象层次结构 TLiveThing=classprotected FTest:string; constructor Create(whereLive:string);overload;virtual; constructor Create(haveBone:Boolean);overload;virtual;end;THuman=class(TLiveThing)public constructor Create(whereLive:s
有如下的简单对象层次结构

TLiveThing=class
protected
 FTest:string;
 constructor Create(whereLive:string);overload;virtual;
 constructor Create(haveBone:Boolean);overload;virtual;
end;

THuman=class(TLiveThing)
public
 constructor Create(whereLive:string);overload;override;
 constructor Create(age:integer);overload;
end;

在理论上,如果我实例化THuman,我有2个构造函数,但实际上我有5个构造函数通过代码洞察显示,实际上我想看到3个构造函数,
– 创建(whereLive:String),覆盖
– 创建(年龄:整数)
– 创建(haveBone:整数)

human:=THuman.Create(       <=====in there I have 5 suggestion constructor

为什么我有这种奇怪的行为?如何解决它,因为它太烦人了,我总是检查我的类,我需要实例化,如果我实例化如下

human:=THuman.Create(); <===== it doesnt give me error

我如何完全隐藏我的anchestor构造函数?,因为如果我像上面那样实例化,完全给我一个错误的对象

更新:我也可以在TObject中看到默认的Create参数

解决方法

不关注你的错误构造函数实现,

你的问题是祖先和子类都是在同一个单元中定义的,因此私有/受保护的标准定义在这里不适用.

如果要防止祖先构造函数(在子类中重写)在实例化该类的对象时显示为代码参数,则只需使其成为严格受保护或严格私有部分的成员即可.

用你的例子:

TLiveThing=class
strict protected
 constructor Create(whereLive:string); virtual;
end;

THuman=class(TLiveThing)
public
 constructor Create(whereLive:string); overload; override;
 constructor Create(age:integer); overload;
end;

这将阻止祖先构造函数Create(whereLive:string)在创建子类的实例时显示为参数.

正如David所指出的,这与隐藏默认的Create构造函数无关,它只适用于隐藏自定义构造函数.

(编辑:李大同)

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

    推荐文章
      热点阅读