delphi – 如何覆盖委托方法实现?

delphi – 如何覆盖委托方法实现?,第1张

概述在Delphi 2007中,我使用一个类来实现第二类支持的接口之一.这很有效.德尔福帮助指出: By default, using the implements keyword delegates all interface methods. However, you can use methods resolution clauses or declare methods in your cla 在Delphi 2007中,我使用一个类来实现第二类支持的接口之一.这很有效.德尔福帮助指出:

By default,using the implements keyword delegates all interface
methods. However,you can use methods resolution clauses or declare
methods in your class that implement some of the interface methods to
overrIDe this default behavior.

但是,当我在我的第二个类中声明一个具有其中一个接口方法的匹配签名的方法时,它不会被调用.

我想知道这是不是因为我在创建它时通过另一个界面访问该类.

下面是一个演示我的问题的测试程序:

program Project1;{$APPTYPE CONSolE}type  IInterface1 = interface  ['{15400E71-A39B-4503-BE58-B6D19409CF90}']    procedure AProc;  end;  IInterface2 = interface  ['{1E41CDBF-3C80-4E3E-8F27-CB18718E8FA3}']  end;  TDelegate = class(TObject)  protected    procedure AProc;  end;  TMyClass = class(TInterfacedobject,IInterface1,IInterface2)  strict private    FDelegate: TDelegate;    property Delegate: TDelegate read FDelegate implements IInterface1;  public    constructor Create;    destructor Destroy; overrIDe;    procedure AProc;  end;procedure TDelegate.AProc;begin  writeln('TClassDelegate.AProc');end;constructor TMyClass.Create;begin  inherited;  FDelegate := TDelegate.Create;end;destructor TMyClass.Destroy;begin  FDelegate.Free;  inherited;end;procedure TMyClass.AProc;begin  writeln('TMyClass.AProc');end;var  MyObj : IInterface2;begin    MyObj := TMyClass.Create;   (MyObj as IInterface1).AProc;end.

当我运行这个时,我得到输出:

TClassDelegate.AProc

我想要的是:

TMyClass.AProc

任何帮助赞赏.

解决方法 似乎你必须以这种方式重新声明你的方法:

TMyClass = class(TInterfacedobject,IInterface2)strict private  ....  procedure test();public  ....  procedure  IInterface1.AProc = test;end;procedure TMyClass.test;begin  writeln('TMyClass.AProc');end;

所以用于TMyClass的IInterface1.AProc映射到test()(而不是FDelegate.AProc)结果是TMyClass.AProc

总结

以上是内存溢出为你收集整理的delphi – 如何覆盖委托方法实现?全部内容,希望文章能够帮你解决delphi – 如何覆盖委托方法实现?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1253664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存