6
这似乎是一个非常愚蠢的问题,但我不知道这是为什么,甚至允许编译:为什么Delphi允许构造函数参数不正确?
program ConstructorWithParam;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TThing = class(TObject)
private
FParent: TObject;
public
constructor Create(const AParent: TObject);
end;
{ TThing }
constructor TThing.Create; // <- WTF? Why does the compiler not complain?
begin
FParent := AParent;
end;
var
Thing: TThing;
begin
try
Thing := TThing.Create(TObject.Create);
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
我用Delphi XE5并没有对其它版本测试。 谢谢。
谢谢肯,我已经使用了德尔福15年,我不知道这一点!我同意 - 我更喜欢接口来匹配实现。我认为唯一不合时宜的是重载方法? –
TurboPASCAL 5.0引入了这个。 –
请注意,如果未指定参数(如果指定了它们必须匹配或者编译器会抱怨),编译器才会允许这样做。这是Pascal的一个语言功能,在Delphi之前。 –