HMc*_*McG 1 delphi oop records
我有一个相对复杂的数据结构来建模.我想用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 ~~~~~~
Run Code Online (Sandbox Code Playgroud)
我目前在单元的Interface部分声明了所有这些.我更喜欢只有主记录结构对使用该单元的任何东西都可见,并且目前所有子记录也是可见的.如果我将记录声明移动到Implementation部分,那么我会遇到编译器错误.我如何进行重组,以便在主记录之前声明子记录但子记录未发布?
您可以执行以下操作之一:
1)在单独的"子单元"中声明"子记录",因此只有在"使用"子句中声明"子单元"时,"子记录"类型才可用.这并不是您正在寻找的,因为"子记录"可以对其他单位可见,但它提供了一定程度的隐藏,因为必须明确声明"子单位"以达到"子记录"定义.
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1173 次 |
| 最近记录: |