这似乎是一个非常愚蠢的问题,但我不知道为什么甚至允许编译:
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并且没有在其他版本上测试过.
谢谢.
解决方法
表单类中的第一个声明被认为是正确的.实现版本不需要识别所需的参数;它们是由原始声明承担的.这是语言本身的一部分.
这是一个很好的例子来说明这一点:
type TMyClass = class (Tobject) procedure DoSometimg(DoA,DoB: Boolean); end;
执行:
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;
我个人更喜欢使实现和接口声明匹配,因为它使得更容易识别参数而不会在代码编辑器中跳转.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。