gpt4 book ai didi

delphi - 如何将接口(interface)添加到尚未包含 TInterfacedObject 的类层次结构中?

转载 作者:行者123 更新时间:2023-12-01 16:16:03 24 4
gpt4 key购买 nike

以下示例展示了如何开始针对接口(interface)进行编码:

来自

TMyObject = class
function Add(a, b: integer): integer;
end;

IInterface = interface
['{BFC7867C-6098-4744-9774-35E0A8FE1A1D}']
function Add(a, b: integer): integer;
end;

TMyObject = class (TInterfacedObject, IInterface
function Add(a, b: integer): integer;
end;

但是如果该类有一个祖先,例如 TMyClassDerivedDirectlyFromTObjectSoItsGotNothingInItAtAll ,我该如何管理?

TMyObject = class(TMyClassDerivedDirectlyFromTObjectSoItsGotNothingInItAtAll)
function Add(a, b: integer): integer;
end;

最佳答案

当您有一个实现接口(interface)的类时,该类必须提供三个方法:_AddRef_ReleaseQueryInterface。如果您查看TInterfacedObject代码,您会注意到那里的那些方法。事实上,TInterfacedObject的存在只是为了更容易地创建新的接口(interface)实现类。

如果您无法从TInterfacedObject继承新类,则必须自己提供这些方法。例如,您可以将TInterfacedObject实现复制到您的类中,那么您的类将成为接口(interface)实现者。

关于delphi - 如何将接口(interface)添加到尚未包含 TInterfacedObject 的类层次结构中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18761716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com