Oop delphi中的许多接口

Oop delphi中的许多接口,oop,delphi,interface,Oop,Delphi,Interface,我希望继承一些D类,并实现接口A、B和C的所有属性和方法。请用Delphi中的一个示例帮助我 我使用delphixe7 一个类如何实现多个接口? 我正在尝试类似的东西: Unit1 Type IRefresher = Interface ['{B289720C-FFA4-4652-9F16-0826550DFCF9}'] procedure Refresh; function getRefreshed: boolean; property R

我希望继承一些D类,并实现接口A、B和C的所有属性和方法。请用Delphi中的一个示例帮助我

我使用delphixe7 一个类如何实现多个接口? 我正在尝试类似的东西:

Unit1
Type
   IRefresher = Interface
      ['{B289720C-FFA4-4652-9F16-0826550DFCF9}']
      procedure Refresh;
      function getRefreshed: boolean;
      property Refreshed:Boolean read getRefreshed;
   End;




它不起作用,因为有许多错误,例如“在基类中找不到刷新”

从代码中删除
覆盖
字,因为没有明确的
接口
方法实现。您在代码中使用的
override
指令仅适用于将由当前类实现的祖先类的
虚拟
抽象
)或
动态
类方法

下面的示例演示了使用接口的伪抽象类(代码示例)重写指令的意义:


对此,您至少有3个实现选项:

1) 虚拟和抽象方法。在这种情况下,不能实例化此类,必须重写子类中的抽象方法。这些方法如下所示:

type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String); virtual; abstract;
  end;
type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String); virtual;
  end;

  implementation

  procedure TGovernmentCustomer.SetName(Value: String);
  begin
    // do something here. You can also leave it empty
  end;
type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String);
  end;

  implementation

  procedure TGovernmentCustomer.SetName(Value: String);
  begin
    // do something here. This will be the behavior of all instances of this class and descendant classes if they exist
  end;
一旦方法是抽象的,就没有实现

2) 虚拟方法。在这种情况下,您可以实例化这个类,并且可以重写子类中的一些虚拟方法。这些方法如下所示:

type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String); virtual; abstract;
  end;
type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String); virtual;
  end;

  implementation

  procedure TGovernmentCustomer.SetName(Value: String);
  begin
    // do something here. You can also leave it empty
  end;
type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String);
  end;

  implementation

  procedure TGovernmentCustomer.SetName(Value: String);
  begin
    // do something here. This will be the behavior of all instances of this class and descendant classes if they exist
  end;
3) 静态方法。在这种情况下,您可以实例化这个类,并且不能覆盖子类中的静态方法。这些方法如下所示:

type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String); virtual; abstract;
  end;
type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String); virtual;
  end;

  implementation

  procedure TGovernmentCustomer.SetName(Value: String);
  begin
    // do something here. You can also leave it empty
  end;
type
  TGovernmentCustomer = class(TInterfacedObject, ICustomer, IRecorder, IRefresher)
    a: String;
  public
    procedure SetName(Value: String);
  end;

  implementation

  procedure TGovernmentCustomer.SetName(Value: String);
  begin
    // do something here. This will be the behavior of all instances of this class and descendant classes if they exist
  end;
最后一点注意:案例(3)的性能最好。在接口上调用虚拟方法会造成性能损失,这可能与您的特定应用程序有关,也可能与您的特定应用程序无关


PS:正如Stefan所指出的,我与另一个SO问题的链接是错误的。但是,您可以从Andreas Hausladden的博客中了解通过接口调用的虚拟方法的性能:

只需删除“override”指令即可。“override”用于重写祖先类中声明为“virtual”的方法。您的8个方法都不存在于祖先类“TInterfacedObject”中。哦,谢谢,但现在我感到困惑。如果我删除覆盖,如何保证我不会收到抽象错误?你知道吗?你的类实现了所有的接口方法。在实现接口方法时,您不需要指定
override
,只需指定
virtual
类方法即可。此代码中没有抽象错误。您链接到的SO问题与接口问题背后的虚拟方法无关。是的,我认为您是对的Stefan。我抓住了错误的答案。我等一下看看能不能找到正确的。谢谢。Stefan,我想我有一个问题的链接来解释这个问题,但碰巧我找不到。无论如何,答案会更新为Andreas Hausladden博客的链接,解释性能差异背后的原因。再次感谢!