Delphi 是否为简单的常用操作提供编译器生成的代码?

Does Delphi provide compiler generated code for simple common operations?

抱歉,如果这是一个新手问题(但我是 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++ 背景,我想知道为什么编译器不能简单地生成上述样板代码片段?

接口中声明的所有内容都必须由 class 实现。 在您的情况下, Create/Destroy 似乎没有必要出现在界面中。 Delphi 中的每个 class 都将继承自已经具有 Create/Destroy.

的 TObject

在接口中,您应该放置那些将扩展实现者的函数 class。

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

从某种意义上说,Delphi 已经 满足了您的要求。只需完全省略构造函数和析构函数声明即可。当一个 class 派生自另一个 class 时,它会自动继承基础 class 的构造函数和析构函数,除非你自己 override 它们。在您的示例中,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.