1

我有一个相对复杂的数据结构来建模。我想用 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 ~~~~~~

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

4

2 回答 2

6

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;
于 2010-02-21T01:00:41.210 回答
1

你不能这样做,因为即使信息不需要直接提供给单元外的其他代码,如果你要在某处使用主记录,编译器仍然需要知道子记录类型.

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

于 2010-02-20T19:06:51.410 回答