列表中的对象需要访问父列表属性 [英] Objects in a list needs to access parent list properties

查看:48
本文介绍了列表中的对象需要访问父列表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Delphi的设计问题。

This is a design question for Delphi.

对象A包含一组变量,并同时存储一系列类型为B的对象。类型B的对象需要访问容器中的公共变量对象A。对象B的一个选项是保留对容器对象A的引用,但这似乎导致将对A的引用存储为TObject,并每次都将对对象A的大小写存储以访问变量。还有其他可能的设计解决方案吗?

An Object A contains a set of variables and at the same time stores a list of Objects of type B. Objects of type B need to access the common variables in the container Object A. One option is for Objects B to hold a reference to the container Object A but this seems to result in storing the reference to A as a TObject and casing each time to Object A in order to access the variables. Any other possible design solutions?

Object B
  // Possible solution
  ref to Object A

Object A
  x : integer
  list of B

每个B需要访问x

推荐答案

您需要前向类型声明:

type
  TContainer = class; // forward declaration

  TItem = class
  private
    FContainer: TContainer;
    ....
  end;

  TContainer = class
  private
    // list of items
  end;

前向声明既可以是容器,也可以是项目,无论哪一个都没关系。

The forward declaration can be either the container or the item, it doesn't much matter which.

在文档中阅读所有相关内容: http://docwiki.embarcadero.com/RADStudio/en/Classes_and_Objects#Forward_Declarations_and_Mutually_Dependent_Classes

Read all about this in the docs: http://docwiki.embarcadero.com/RADStudio/en/Classes_and_Objects#Forward_Declarations_and_Mutually_Dependent_Classes

这篇关于列表中的对象需要访问父列表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆