这似乎是一个非常愚蠢的问题,但我不知道为什么甚至允许编译:
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.
Run Code Online (Sandbox Code Playgroud)
我正在使用Delphi XE5并且没有在其他版本上测试过.谢谢.
表单类中的第一个声明被认为是正确的.实现版本不需要识别所需的参数; 它们是由原始声明承担的.这是语言本身的一部分.
这是一个很好的例子来说明这一点:
type
TMyClass = class (Tobject)
procedure DoSometimg(DoA, DoB: Boolean);
end;
Run Code Online (Sandbox Code Playgroud)
执行:
procedure TMyClass.DoSomething; // Note both parameters missing
begin
if DoA then // Note not mentioned in implementation declaration
DoOneThing; // but still can be used here
if DoB then
DoAnotherThing;
end;
Run Code Online (Sandbox Code Playgroud)
我个人更喜欢使实现和接口声明匹配,因为它使得更容易识别参数而不会在代码编辑器中跳转.
| 归档时间: |
|
| 查看次数: |
155 次 |
| 最近记录: |