【发布时间】:2018-08-25 18:50:41
【问题描述】:
我有一对类 A 和 B,它们必须相互引用。也就是说A的接口使用子句必须提到B的单位,B的接口使用子句必须提到A的单位。这在Delphi中是循环引用,不允许。我正在使用here 找到的方法来解决循环问题。基本上,A 对 B 的引用按预期存在,但 B 对 A 的引用被降级为 TObject 引用,并且 B 使用帮助器类将对 A 的引用转换为正确的类型。这里有代码sn-ps来说明:
unit A;
interface
uses
B;
type
TA = class(TObject)
public
Name: string;
B: TB;
end;
implementation
end.
unit B;
interface
type
TB = class(TObject)
public
Name: string;
protected
fA: TObject;
end;
implementation
end.
unit BHelper;
interface
uses
A,
B;
type
TBHelper = class helper for TB
private
function GetA: TA;
procedure SetA(Value: TA);
public
property A: TA read GetA write SetA;
end;
implementation
function TBHelper.GetA: TA;
begin
Result := TA(fA);
end;
procedure TBHelper.SetA(Value: TA);
begin
fA := a;
end;
end.
我实际上有几对这样的课程。这是很多帮助类,它们都在做同样的事情。所以我尝试编写一个通用的帮助类,可以通过参数化来帮助像 B 这样的类:
unit GenericHelper;
interface
uses
A,
B;
type
TGenericHelper<T1, T2> = class helper for T2 //Error: Type parameters not allowed
private
function GetA: T1;
procedure SetA(Value: T1);
public
property A: T1 read GetA write SetA;
end;
implementation
function TGenericHelper<T1, T2>.GetA: T1;
begin
Result := T1(fA); //Error: undeclared parameter fA
end;
procedure TGenericHelper<T1, T2>.SetA(Value: T1);
begin
fA := a; //Error: undeclared parameter fA
end;
end.
我的问题是我对泛型类的了解不够,无法知道这是否可行。我不知道如何解决代码中显示的错误。
有没有办法做到这一点?
完全披露:情况实际上比这更丰富。这些对排列在 A-B-C-D 等层次结构中,其中 A 持有对 B 的引用,B 持有对 A 和 C 的引用,C 持有对 B 和 D 的引用,D 持有对 C 的引用。这些是一对多的其中 A 实际上持有 B 的列表,每个 B 持有 C 的列表,每个 C 持有 D 的列表。理想的解决方案不会排除这些关系。我也更喜欢每个单元一个类,而不是将相关的类组合成一个单元。
谢谢
【问题讨论】:
-
TFriend的声明在哪里?fChild是TFriend的成员吗?fParent?看起来您已经使用与实际类型名称冲突的泛型类型名称声明了泛型(即:与实际类型名称相同并且正在隐藏)。您究竟想在这里完成什么? -
为了清楚起见,我已经重写了这个问题。
-
为什么不把 A/B 放在同一个单元中并使用前向声明?