Delphi中隐藏的嵌套记录信息

发布于 2024-08-22 03:31:52 字数 1310 浏览 3 评论 0原文

我有一个相对复杂的数据结构需要建模。我想用 Delphi 中的记录结构来完成此操作,并且该结构足够复杂,足以证明将其拆分为嵌套记录是合理的。一个简化的例子:

    type
      TVertAngle = record
      strict private
        fDecDegrees: Double;
        fDegrees: integer;
        fMinutes: integer;
        fDeciSeconds: integer;
        function GetAngle: Double;
        function GetRadians: Double;
      public
        Valid: Boolean;
        procedure SetAsString(const Value: string; const AngleType: TInfoUnits);
        property DecDegrees: Double read GetAngle;
        property Radians: Double read GetRadians;
      end;

~~~~ other sub record declarations ~~~~~~

  TDataRecord = record
  strict private
    fHorzDistance: Double;
    fLeicaData: TRawMessageData;
    fUpdateTime: TDateTime;
    function DecodeGsi8(GsiWord: string): TGSiWord;
    function DecodeGsi16(GsiWord: string): TGSiWord;
  public
    GsiWord: TGSiWord;
    Valid: Boolean;
    InputMode: TDataModes;
    HorzAngle: THorzAngle;
    VertAngle: TVertAngle;
    HorzRange: TDistance;
    SlopeRange: TDistance;
    PrismOffset: TConstants;
~~~~ other sub record instances~~~~~~
    function SetMessage(RawMessage: string): Boolean;
~~~~ more stuff ~~~~~~

我目前在单元的接口部分声明了所有这些。我希望只有主记录结构对使用该单元的任何内容都是可见的,并且目前所有子记录也是可见的。如果我将记录声明移至实现部分,则会出现编译器错误。如何重组以便在主记录之前声明子记录但不发布子记录?

I have a relatively complicated data structure to model. I would like to do this with a record structure in Delphi, and the structure is complicated enough to justify splitting this into nested records. A simplified example:

    type
      TVertAngle = record
      strict private
        fDecDegrees: Double;
        fDegrees: integer;
        fMinutes: integer;
        fDeciSeconds: integer;
        function GetAngle: Double;
        function GetRadians: Double;
      public
        Valid: Boolean;
        procedure SetAsString(const Value: string; const AngleType: TInfoUnits);
        property DecDegrees: Double read GetAngle;
        property Radians: Double read GetRadians;
      end;

~~~~ other sub record declarations ~~~~~~

  TDataRecord = record
  strict private
    fHorzDistance: Double;
    fLeicaData: TRawMessageData;
    fUpdateTime: TDateTime;
    function DecodeGsi8(GsiWord: string): TGSiWord;
    function DecodeGsi16(GsiWord: string): TGSiWord;
  public
    GsiWord: TGSiWord;
    Valid: Boolean;
    InputMode: TDataModes;
    HorzAngle: THorzAngle;
    VertAngle: TVertAngle;
    HorzRange: TDistance;
    SlopeRange: TDistance;
    PrismOffset: TConstants;
~~~~ other sub record instances~~~~~~
    function SetMessage(RawMessage: string): Boolean;
~~~~ more stuff ~~~~~~

I currently have all this declared in the Interface section of the unit. I would prefer if only the main record structure was visible to anything using the unit, and at the moment all the sub records are also visible. If I move the record declarations into the Implementation section then I get compiler errors. How do I restructure so that I declare the sub-records prior to the main record but the sub-records are not published?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦慕雪 2024-08-29 03:31:52

您可以执行以下操作之一:

1) 在单独的“子单元”中声明“子记录”,因此仅当“子单元”在“uses”子句中声明时“子记录”类型才可用。这并不完全是您正在寻找的,因为“子记录”可以对其他单元可见,但它提供了一定程度的隐藏,因为必须明确声明“子单元”才能达到“子记录”定义。

2) 将“子记录”声明为私有嵌套类型,如下所示:

type
  TMainRec = record
    private type
      TSubRec = record
        FSubField: Integer;
        procedure SubMethod;
      end;
    private
      FSubRec: TSubRec;
  end;

implementation

{ TMainRec.TSubRec }

procedure TMainRec.TSubRec.SubMethod;
begin
...
end;

You can do one of the following:

1) Declare "sub records" in a separate "sub unit", so the "sub record" types are available only if the "sub unit" is declared in the "uses" clause. That is not exactly what you are looking for, since the "sub records" can be made visible for other units, but it provides some degree of hiding since the "sub unit" must be explicitely declared to reach "sub record" definitions.

2) Declare "sub records" as private nested types as follows:

type
  TMainRec = record
    private type
      TSubRec = record
        FSubField: Integer;
        procedure SubMethod;
      end;
    private
      FSubRec: TSubRec;
  end;

implementation

{ TMainRec.TSubRec }

procedure TMainRec.TSubRec.SubMethod;
begin
...
end;
柒七 2024-08-29 03:31:52

您不能这样做,因为即使信息不需要直接可供单元外部的其他代码使用,如果您要在某处使用主记录,编译器仍然需要了解子记录类型。

您可以尝试的一件事是在另一个单元中声明其他类型,然后不在其他地方使用该其他单元。但这并不能真正解决问题;它只是隐藏了一点。

You can't do that, because even if the information doesn't need to be directly available to other code outside the unit, the compiler still needs to know about the sub-record types if you're going to use the main record somewhere.

One thing you could try is declaring the other types in another unit and then not using that other unit anywhere else. That doesn't really solve the problem, though; it just hides it a little.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文