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

标签 delphi circular-dependency

我在两个不同的单元中创建了两个不同的类,我如何创建循环引用?在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/

相关文章:

Delphi 10.4.2 "could not find wizard gexperts"启动错误

delphi - Delphi 5 IDE 可以在 Windows 10 上运行吗?

c# - 我的 C# 项目中的循环引用

java - 使用 Spring Boot 和 Waffle 配置 Spring Security 时发生循环依赖错误

angular - 复杂的 TypeScript 循环依赖问题

delphi - 在 Timage 中显示图像列表项

由于环境变量(HP笔记本电脑),delphi XE2无法在我的计算机上编译任何项目

delphi - 寻找一个简单的Delphi图形组件

f# - 循环引用和构造函数

c++ - 如何解决在仅 header 库 (C++) 中定义的结构对其他结构类型的循环依赖?