2017-08-04 25 views
-9

对不起,如果这是一个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)可用的​​宏套件和工具来克服这个问题? (你可以在评论中张贴建议,因为这将是脱离主题在这里要求这样的工具)。
+1

“*我不知道为什么编译器不能简单地生成上述样板代码片段*?” - 的IDE有诸如[Class Completion](http://docwiki.embarcadero.com/RADStudio/en/Using_Class_Completion)和[Live Templates](http://docwiki.embarcadero.com/RADStudio/en/Using_Live_Templates)等功能来处理这些的东西。 –

+2

@RemyLebeau谢谢你。使用IDE功能修复(解决方法)编译器缺陷真的是一个好主意吗?你怎么看? – user0042

+2

这不是编译器缺陷。编译器的责任不是编译用户代码,而是编译用户代码。 *生成用户代码是IDE的优点。所有主要的IDE都有与之相关的功能。你理解IDE和编译器之间的区别,不是吗? –

回答

4

从某种意义上说,德尔福已经做你所要求的。完全省略构造函数和析构函数声明。当一个类派生自另一个类时,它会自动继承基类的构造函数和析构函数,除非你自己。在你的例子中,override是多余的,可以删除。

InterfaceUnit.pas

unit InterfaceUnit; 

interface 

type 
    IMyInterface = interface(IInterface) 
    procedure DoStuff(withStuff : Stuff); 
    end; 

    TMyInterfaceHelperClass = class(TInterfacedObject, IMyInterface) 
    public 
    procedure DoStuff(withStuff : Stuff); virtual; abstract; 
    end; 

implementation 

{TMyInterfaceHelperClass} 

end. 

ImplementationUnit.pas

unit ImplementationUnit; 

interface 

uses InterfaceUnit; 

type 
    TMyInterfaceImplementationClass = class(TMyInterfaceHelperClass) 
    public 
    procedure DoStuff(withStuff : Stuff); override; 
    end; 

implementation 

{TMyInterfaceImplementationClass} 

procedure TMyInterfaceImplementationClass.DoStuff(withStuff : Stuff); 
begin 
    {Combine TMyInterfaceHelperClass to do extraordinary stuff with Stuff} 
end; 

end. 
+0

FWIW,不需要两个类:'TMyInterfaceHelperClass'和'TMyInterfaceImplementationClass'。特别是虚拟类('TMyInterfaceHelperClass'是一个虚拟类)是完全不必要的。每个类都可以通过声明它并实现接口的方法来实现'TMyInterface'。不需要某种虚拟基类。 –

+0

@RudyVelthuis:在OP的代码中,助手类“拥有许多标准方法来处理内容”,并与后代共享。我在答案中将代码留在代码中。 –

0

接口中声明的所有内容都必须由该类实现。 在你的情况下,创建/销毁似乎不需要在界面中。 Delphi中的每个类都将继承自TObject,它已经具有Create/Destroy。

在一个接口中,你应该把这些功能扩展到实现类。

顺便说一句你在Create()中放置一个Destroy吗?

+0

我没有把'constructor' /'destructor'声明放入界面,请再次阅读代码。 – user0042

+0

ops我的错误我没有正确读取接口代码,messigng它与实现... –

+0

@ user0042你没有把ctor和dtor放在接口类型中,但是你确实把它们放在类声明中的接口_section_的单位。正如Daniel _correctly_指出的那样:根据你的实施情况,ctor和dtor不会为你的行为添加任何**;他们完全是多余的。只需删除声明和实施。你***不需要写出样板代码_at all_! –

相关问题