gpt4 book ai didi

delphi - 两个类有两个循环引用

转载 作者:行者123 更新时间:2023-12-02 10:52:05 25 4
gpt4 key购买 nike

我在两个不同的单元中创建了两个不同的类,我如何创建循环引用?在Delphi中(类位于不同的单元中)

单元1:

Uses unit2;

type Ta = class(tobject)
public
b:Tb;
end;

单元2:

type Tb = class(tobject)
public
a:Ta;
end;

最佳答案

我想你的意思是我该如何摆脱它们!

将它们放在一个文件中,根据您的结构,您可能会得到一个答案。

type tb = class;

type Ta = class(TObject)
public
b:Tb;
end;

type Tb = class(TObject)
public
a:Ta;
end;

其他方式是根据情况而定的,例如您可以抽象出一个可以拥有 Ta 或 Tb 的类,或者一个类可以由 Ta 或 Tb 拥有...

不过我建议你看看 Interfaces....

好吧,两个不同的文件

好吧,不,三个......

Unit3;
type tc = class(TObject)
public c:Tc;
end;

Unit1;
type Ta = class(TObject)
public
b:Tc;
end;

Unit2;
type Tb = class(TObject)
public
a:Tc;
end;

或者

Unit3;
type Ic = interface; end;

Unit1;
type Ta = class(TObject)
public
b:Ic;
end;

Unit2;
type Tb = class(TObject)
public
a:Ic;
end;

找到共同点,将其放在第三个单元中,让其他两个单元基本上使用它。抛开其他的不谈,它会给你一个更好的设计。

关于delphi - 两个类有两个循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8716202/

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