对不起,如果这是一个noob问题(但我是德尔福noob)。Delphi是否为简单的常见操作提供编译器生成的代码?
让我们假设下面的代码
InterfaceUnit.pas
unit InterfaceUnit;
interface
type
IMyInterface = interface(IInterface)
procedure DoStuff(withStuff : Stuff);
end;
TMyInterfaceHelperClass = class(TInterfacedObject, IMyInterface)
public
constructor Create();
destructor Destroy();
procedure DoStuff(withStuff : Stuff); virtual; abstract;
strict protected
{Have tons of standard ways how to process Stuff}
end;
implementation
{TMyInterfaceHelperClass}
constructor TMyInterfaceHelperClass.Create();
begin
inherited Create();
end;
destructor TMyInterfaceHelperClass.Destroy();
begin
inherited Destroy();
end;
{Have tons of standard ways how to process Stuff implemented here}
end.
ImplementationUnit.pas
unit ImplementationUnit;
interface
uses InterfaceUnit;
type
TMyInterfaceImplementationClass = class(TMyInterfaceHelperClass)
public
{*******************************************************************
* The Question is: ...
*******************************************************************}
constructor Create();
destructor Destroy(); override;
{*******************************************************************}
procedure DoStuff(withStuff : Stuff); override;
end;
implementation
{TMyInterfaceImplementationClass}
{*******************************************************************
* ... Why do I need to write that boilerplate code all the time?
*******************************************************************}
constructor TMyInterfaceImplementationClass.Create();
begin
inherited Create();
end;
destructor TMyInterfaceImplementationClass.Destroy();
begin
inherited Destroy();
end;
{*******************************************************************}
procedure TMyInterfaceImplementationClass.DoStuff(withStuff : Stuff);
begin
{Combine TMyInterfaceHelperClass to do extraordinary stuff with Stuff}
end;
end.
让我们跳出代码,并继续与纯文本。
因为我来自C++的背景,我想知道为什么编译器不能简单地生成上面提到的样板代码段?
- 有没有特别的原因我错过了,为什么上面提到的代码不能像编译器那样以任何体面的C++编译器的方式生成?
- 至于目前的情况,我相信有RAD Studio(10)可用的宏套件和工具来克服这个问题? (你可以在评论中张贴建议,因为这将是脱离主题在这里要求这样的工具)。
“*我不知道为什么编译器不能简单地生成上述样板代码片段*?” - 的IDE有诸如[Class Completion](http://docwiki.embarcadero.com/RADStudio/en/Using_Class_Completion)和[Live Templates](http://docwiki.embarcadero.com/RADStudio/en/Using_Live_Templates)等功能来处理这些的东西。 –
@RemyLebeau谢谢你。使用IDE功能修复(解决方法)编译器缺陷真的是一个好主意吗?你怎么看? – user0042
这不是编译器缺陷。编译器的责任不是编译用户代码,而是编译用户代码。 *生成用户代码是IDE的优点。所有主要的IDE都有与之相关的功能。你理解IDE和编译器之间的区别,不是吗? –