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

Delphi是否为简单的通用操作提供编译器生成的代码?,delphi,delphi-2010,Delphi,Delphi 2010,抱歉,如果这是一个noob问题(但我是delphinoob) 让我们假设以下代码 接口单位pas unit InterfaceUnit; interface type IMyInterface = interface(IInterface) procedure DoStuff(withStuff : Stuff); end; TMyInterfaceHelperClass = class(TInterfacedObject, IMyInterface

抱歉,如果这是一个noob问题(但我是delphinoob)

让我们假设以下代码

接口单位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.
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.
实施单元.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.
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++背景时,我想知道为什么编译器不能简单地生成上面提到的样板代码片?< /P>
    <> LI>有什么特别的原因我错过了,为什么上面的代码不能像编译器那样用任何一个体面的C++编译器来做?
  • 至于目前的情况,我相信RAD Studio(10)可以使用宏套件和工具来解决这个问题?(你可以在评论中发表建议,因为在这里要求使用这些工具是不合主题的)

    • 接口中声明的所有内容都必须由类实现。 在您的情况下,Create/Destroy似乎不必位于接口中。 Delphi中的每个类都将继承TObject,而TObject已经有一个Create/Destroy

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

      顺便说一句,您是否在Create()中添加了销毁功能?

      从某种意义上说,Delphi已经满足了您的要求。只需完全省略构造函数和析构函数声明。当一个类从另一个类派生时,它会自动继承基类的构造函数和析构函数,除非您自己重写它们。在您的示例中,
      覆盖
      是冗余的,可以删除

      接口单位

      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.
      
      实现单元

      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.
      

      “我想知道为什么编译器不能简单地生成上面提到的样板代码片段?”-IDE具有像和这样的功能,可以处理这类事情。@RemyLebeau对此表示感谢。用IDE特性修复(解决)编译器缺陷真的是个好主意吗?你认为呢?这不是编译器的缺陷。编译器首先不负责生成用户代码,而只负责编译用户代码。生成用户代码是IDE的优势所在。所有主要的IDE都有与此相关的特性。你确实知道IDE和编译器之间的区别,不是吗?@ ReMe。这不是编译器要执行的任务。“那么C++标准COMITEE对此有什么错误?@ USER442:你知道C++编译器是什么样的锅炉板用户代码,就像你在你的问题中描述的那样?没有,因为这不是C++标准的一部分。您所要求的与编译器在编译时生成构造函数和赋值运算符的默认实现完全不同。这些实现没有保存回用户的源代码文件。我没有将
      构造函数
      /
      析构函数
      声明放入接口中,请重新阅读代码。操作我的错误我没有正确阅读接口代码,将其与实现混淆…@user0042您没有将ctor和dtor放入接口类型中,但您确实将它们放在了单元接口部分的类声明中。正如Daniel正确指出的那样:基于您的实现,ctor和dtor对您的行为没有任何影响;它们完全是多余的。只需删除声明和实现。您根本不需要编写样板代码!FWIW,不需要两个类:
      TMyInterfaceHelperClass
      TMyInterfaceImplementationClass
      。特别是一个虚拟类(
      tmyinterfaceheloperclass
      是一个虚拟类)是完全不必要的。每个类都可以通过声明并实现接口的方法来实现
      TMyInterface
      。不需要任何类型的虚拟基类。@RudyVelthuis:在OP的代码中,helper类“有大量的标准方法来处理内容”,并与后代共享。我在回答时把那部分从代码中漏掉了。