熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

DELPHI基礎教程:開發Delphi對象式數據管理功能(三)[2]

2022-06-13   來源: Delphi編程 

  ⑵ Destroy方法的實現

  Destroy方法是TFiler對象的析構函數它的作用就是釋放動態內存

  destructor TFilerDestroy;

  begin

  if FBuffer <> nil then FreeMem(FBuffer FBufSize)

  end;

   TWriter對象

  TWriter 對象是可實例化的往流中寫數據的Filer對象TWriter對象直接從TFiler繼承而來除了覆蓋從TFiler繼承的方法外還增加了大量的關於寫各種數據類型(如IntegerString和Component等)的方法TWriter對象和TReader 對象配合使用將使對象讀寫發揮巨大作用

   TWriter對象的屬性和方法

   Position屬性

  聲明

property Position: Longint;

  TWriter對象的Position屬性表示相關聯的流中的當前要寫的位置TReader 對象也有這個屬性但與TReader對象不同的是TWriter對象的Position的值比流的Position值小這一點一看屬性實現就清楚了

   RootAncesstor屬性

  聲明

property RootAncestor: TComponent;

  RootAncestor屬性表示的是Root屬性所指的部件的祖先如果Root 是繼承的窗體Writer對象將窗體擁有部件與祖先窗體中的相應部件依次比較然後只寫入那些與祖先中的不同的部件

   Write方法

  聲明

procedure Write(const Buf; Count: Longint)

  Write方法從Buf中往與Writer相關聯的流中寫入Count個字節

   WriteListBegin方法

  聲明

procedure WriteListBegin;

  WriteListBegin方法往Write對象的流中寫入項目列表開始標志該標志意味著後面存儲有一連串的項目Reader對象在讀這一連串項目時先調用ReadListBegin方法讀取該標志位然後用EndOfList判斷是否列表結束並用循環語句讀取項目在調用WriteListBegin方法的後面必須調用WriteListEnd方法寫列表結束標志相應的在Reader對象中有ReadListEnd方法讀取該結束標志

   WriteListEnd方法

  聲明

procedure WriteListEnd;

  WriteListEnd方法在流中寫入項目列表結束標志它是與WriteListBegin相匹配的方法

   WriteBoolean方法

  聲明

procedure WriteBoolean(Value: Boolean)

  WriteBoolean方法將Value傳入的布爾值寫入流中

   WriteChar方法

  聲明

procedure WriteChar(Value: char)

  WriteChar方法將Value中的字符寫入流中

   WriteFloat方法

  聲明

procedure WriteFloat(Value: Extended)

  WriteFloat方法將Value傳入的浮點數寫入流中

   WriteInteger方法

  聲明

procedure WriteInteger(Value: Longint)

  WriteInteger方法將Value中的整數寫入流中

   WriteString方法

  聲明

procedure WriteString(const Value: string)

  WriteString方法將Value中的字符串寫入流中

   WriteIdent方法

  聲明

procedure WriteIdent(const Ident: string)

  WriteIdent方法將Ident傳入的標識符寫入流中

   WriteSignature方法

  聲明

procedure WriteSignature;

  WriteSignature方法將Delphi Filer對象標簽寫入流中WriteRootComponent方法在將部件寫入流之前先調用WriteSignature方法寫入Filer標簽Reader對象在讀部件之前調用ReadSignature方法讀取該標簽以指導讀操作

   WritComponent方法

  聲明

procedure WriteComponent(Component: TComponent)

  WriteComponent方法調用參數Component的WriteState方法將部件寫入流中在調用WriteState之前WriteComponent還將Component的ComponetnState屬性置為csWriting當WriteState返回時再清除csWriting

   WriteRootComponent方法

  聲明

procedure WriteRootComponent(Root: TComponent)

  WriteRootComponent方法將Writer對象Root屬性設為參數Root帶的值然後調用WriteSignature方法往流中寫入Filer對象標簽最後調用WriteComponent方法在流中存儲Root部件

   TWriter對象的實現

  TWriter對象提供了許多往流中寫各種類型數據的方法這對於程序員來說是很重要的功能TWrite對象往流中寫數據是依據不同的數據采取不同的格式的 因此要掌握TWriter對象的實現和應用方法必須了解Writer對象存儲數據的格式

  首先要說明的是每個Filer對象的流中都包含有Filer對象標簽該標簽占四個字節其值為TPFFiler對象為WriteSignature和ReadSignature方法存取該標簽該標簽主要用於Reader對象讀數據(部件等)時指導讀操作

  其次Writer對象在存儲數據前都要留一個字節的標志位以指出後面存放的是什麼類型的數據該字節為TValueType類型的值TValueType是枚舉類型占一個字節空間其定義如下

  TValueType = (VaNull VaList VaInt VaInt VaInt VaEntended VaString VaIdent

  VaFalse VaTrue VaBinary VaSet VaLString VaNil VaCollection)

  因此對Writer對象的每一個寫數據方法在實現上都要先寫標志位再寫相應的數據而Reader對象的每一個讀數據方法都要先讀標志位進行判斷如果符合就讀數據否則產生一個讀數據無效的異常事件VaList標志有著特殊的用途它是用來標識後面將有一連串類型相同的項目而標識連續項目結束的標志是VaNull因此在Writer對象寫連續若干個相同項目時先用WriteListBegin寫入VaList標志寫完數據項目後再寫出VaNull標志而讀這些數據時以ReadListBegin開始ReadListEnd結束中間用EndofList函數判斷是否有VaNull標志

  下面就介紹它們的實現

   TWriter對象屬性的實現

  TWriter對象直接從TFiler對象繼承它只增加了Position和RootAncestor屬性

  RootAncestor屬性在private部分有數據域FRootAncestor存入其值在屬性定義的讀與控制上都是直接讀取該值

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/Delphi/201311/25097.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.