这是两个简单的类,最初都没有关键字(virtual、overload、override、reintroduce):
TComputer = class(TObject)
public
constructor Create(Teapot: Integer);
end;
TCellPhone = class(TComputer)
public
constructor Create(Teapot: Integer; Handle: string);
end;
我将把上述这些定义表示为略短:
TComputer = class(TObject)
constructor Create(Teapot: Integer);
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string);
并且在构造时TCellPhone
只有一个构造函数 ( int
, string
) - 因为祖先构造函数已被隐藏。我将指出以下的可见构造函数TCellPhone
:
- 茶壶:整数;句柄:字符串
现在对于这个问题,前 3 种情况是有道理的,第 4 种情况没有:
1.祖先构造函数被后代隐藏:
TComputer = class(TObject)
constructor Create(Teapot: Integer);
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string);
Teapot: Integer; Handle: string
这是有道理的,祖先构造函数是隐藏的,因为我已经声明了一个新的构造函数。
2.祖先虚构造函数被后代隐藏:
TComputer = class(TObject)
constructor Create(Teapot: Integer); virtual;
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string);
Teapot: Integer; Handle: string
这是有道理的,祖先构造函数是隐藏的,因为我已经声明了一个新的构造函数。
注意:因为祖先是虚拟的:Delphi 会警告你隐藏了虚拟祖先(在前面的隐藏静态构造函数的例子中:没人关心,所以没有警告)。可以通过添加reintroduce来抑制警告(意思是“是的,是的,我正在隐藏一个虚拟构造函数。我 打算这样做。”):
TComputer = class(TObject) constructor Create(Teapot: Integer); virtual; TCellPhone = class(TComputer) constructor Create(Teapot: Integer; Handle: string); reintroduce;
3. 由于重载,祖先构造函数没有隐藏在后代中:
TComputer = class(TObject)
constructor Create(Teapot: Integer);
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string); overload;
Teapot: Integer; Handle: string
Teapot: Integer
这是有道理的,因为后代构造函数是祖先的重载,所以两者都被允许存在。祖先构造函数没有被隐藏。
4.虚拟祖先构造函数没有隐藏在后代中,因为重载 -但仍然收到警告:
这是没有意义的情况:
TComputer = class(TObject)
constructor Create(Teapot: Integer); virtual;
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string); overload;
Teapot: Integer; Handle: string
Teapot: Integer
方法“创建”隐藏基本类型“TComputer”的虚拟方法
这没什么意义。不仅祖先不隐藏,后代超载;它甚至不应该抱怨。
是什么赋予了?