代码之家  ›  专栏  ›  技术社区  ›  user0042

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

  •  -10
  • user0042  · 技术社区  · 7 年前

    让我们假设以下代码

    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.
    

    实现单元.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++背景,我想知道为什么编译器不能简单地生成上述样板代码?

    • 至于目前的情况,我相信有宏套件和工具可供RAD Studio(10)克服这一点?(你可以在评论中发表建议,因为那将是 离题
    2 回复  |  直到 7 年前
        1
  •  4
  •   Remy Lebeau    7 年前

    从某种意义上说,德尔菲 已经 做你想做的事。只需完全省略构造函数和析构函数声明。当一个类从另一个类派生时,它会自动继承基类的构造函数和析构函数,除非您 override 推翻 s是冗余的,可以删除。

    接口单位.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.
    

    实现单元.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.
    
        2
  •  0
  •   Daniel Luyo    7 年前

    接口中声明的所有内容都必须由类实现。