單元四、Object Pascal 介紹

 

4.1 Object Pascal程式單元

 

4.1.1 Object Pascal程式單元的基本架構

打開一個空的表格。

 

unit Unit1; {程式單元的開始及單元名稱}

 

interface {interface部分的開始}

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

 

Type {型態宣告}

TForm1 = class(TForm)

Private

{ Private declarations }

public

{ Public declarations }

end;

 

var {變數宣告}

Form1: Tform1;

 

Implementation {實作部分的開始}

 

{$R *.DFM}

 

end.

 

 

 

4.1.2 變數宣告

 

Var

Form1:TForm1;

ICounter:Interger;

SName:String;

 

 

 

類別

資料型態

說明

整數

Integer

2 bytes-3276832767

Shortint

1 byte-128127

Longint

4 bytes

Byte

1 byte0255

Word

2 byte065535

實數

Single

78位有效數字,需4 bytes

Double

1516位有效數字,需8 bytes

Extended

1920位有效數字,需10 bytes

Comp

1920位有效數字,需8 bytes

Real

1112位有效數字,需6 bytes

布林

Boolean

True or False,需1 bit

字元

Char

ASCII字元

字串

String

最長255ASCII字元

指標

Pointer

未定型的指標

字串指標

Pchar

指向字串的指標

 

 

4.1.3 型態宣告

 

 

4.1.4 uses子句

 

 

 

4.2 程式單元的內涵

實作一個表格,並研究其產生的程式碼。

 

 

unit MainForm;

 

interface

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

DB, DBTables, StdCtrls;

 

type

TForm1 = class(TForm)

ListBox1: TListBox;

Button1: TButton;

Button2: TButton;

Table1: TTable;

DataSource1: TDataSource;

private

{ Private declarations }

public

{ Public declarations }

end;

 

var

Form1: TForm1;

 

implementation

 

 

end.

 

 

Unit1interface部份出現

 

type

TForm1 = class(TForm)

procedure Button2Click(Sender: TObject);

private

 

Unit1implementation部份出現

 

implementation

 

{$R *.DFM}

 

procedure TForm1.Button2Click(Sender: TObject);

begin

end;

 

 

procedure TForm1.Button2Click(Sender: TObject);

begin

ListBox1.Clear;

end;

 

 

4.3 流程控制

 

4.3.1 跳離敘述

 

 

If ThisYearIs = ‘1995’ then

MessageBeep(1)

Else

MessageDlg(‘ThisYearIs不是1995’, mtInformation, [mbOK], 0);

 

 

 

else會自動與最靠近的if配對。

If ThisYearIs = ‘1995’ then

If ThisMonthIs = ‘8’ then

MessageBeep(1)

Else

MessageDlg(‘ThisYearIs不是1995’, mtInformation, [mbOK], 0);

則當1995年且不是8月時顯示『ThisYearIs不是1995

 

If ThisYearIs = ‘1995’ then

Begin

If ThisMonthIs = ‘8’ then

MessageBeep(1)

End

Else

MessageDlg(‘ThisYearIs不是1995’, mtInformation, [mbOK], 0);

則當不是1995年時顯示『ThisYearIs不是1995

 

 

Case StrToInt(thisMonthIs) of

1, 3, 5 , 7, 9, 11:

MessageDlg(‘ThisMonthIs是奇數月’, mtInformation, [mbOk], 0);

2, 4, 6, 8, 10, 12:

MessageDlg(‘ThisMonthIs是偶數月’ mtInformation, [mbOk], 0);

Else

MessageDlg(‘ThisMonthIs有錯誤’, mtInformation, [mbOk], 0);

End;

 

4.3.2 迴圈敘述

 

I:=1;

Repeat

Writeln(I);

Until I = 10;

 

I:=1;

While I<= 1 do

Begin

I:=I+1;

Writeln(I);

end;

 

for I:= 1 to 10 do

Writeln(I);

 

for I:= 1 to 10 do

for J:= 1 to 5 do

Writeln(I*J);

 

 

4.3.3 完成範例程式

 

 

procedure TForm1.Button1Click(Sender: TObject);

begin

Table1.First;

While not Table1.EOF do

Begin

ListBox1.Items.Add(Table1.FieldByname('NAME').Text);

Table1.Next;

End;

end;

 

 

 

 

4.4 加入第二個表格(the second form)

 

 

此時的程式編輯器的內容如下:

 

unit SndForm;

 

interface

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

DBCtrls, DB, DBTables, StdCtrls;

 

Type

TForm2 = class(TForm)

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

DBImage1: TDBImage;

DBText1: TDBText;

DBText2: TDBText;

DBText3: TDBText;

DBText4: TDBText;

private

public

end;

 

var

Form2: TForm2;

 

Implementation

 

{$R *.DFM}

end.

 

 

procedure TForm1.ListBox1DblClick(Sender: TObject);

begin

Form2.ShowModal;

end;

 

 

implementation

 

uses

SndForm;

 

{$R *.DFM}

 

 

 

4.5 程序和函式

 

4.5.1 表頭部份

 

procedure由保留字procedure開始,參數放於刮號內。

Procedure setDBName(Name1: string; Name2:string; Name3:string);

Procedure setDBName(Name1, Name2, Name3:string);

 

function由保留字function開始,參數放於刮號內,後面加型態宣告。

function getDBName(Name1, Name2, Name3:string):string;

 

4.5.2 程式區塊

 

 

Type

TCount = Integer;

TPrimaryColor = (Red, Yellow, Blue);

TTestIndex = 1..100;

TTextValue = -99..99;

TTestList = array[TTestIndex] of TTestValue;

TCharVal = Ord(‘A’)..Ord(‘Z’);

TDays = (Modday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

 

 

 

4.5.3 函式值的回傳

 

  1. 把回傳的值指定給函式名稱。
  2.  

    Function GetRecordNumber: Integer;

    Begin

    GetRecordNumber:=Table.RecordCount;

    End;

     

  3. 把回傳的值指定給Result變數(此變數不需宣告即可使用)。

 

Function GetRecordNumber: Integer;

Begin

Result:=Table.RecordCount;

End;

 

 

4.5.4 在程式中加入程序或函式

 

  1. 程序或函式把的表頭部份放在程式單元的interface部份。
  2. 把宣告這個程序或函式的程式單元的名稱放在存取這個程序或函式的程式單元的uses子句中。

程序或函式必須在所有使用他的程式之前宣告。

使用前置宣告的程序或函式可以把其implementation部份放在程式的任何一個地方。他可以用來產生相互遞迴的功能,即在A函式中有B函式,同時B函式中也有A函式。

 

Function GetRecordNumber:Integer; forward;

 

4.5.5 傳遞參數

 

 

procedure SetformData(aTblName:string);

 

 

Procedure GetTableName(var aTblName:string);

 

 

procedure SetformData(const aTblName:string);

 

 

5.6 完成範例程式

 

 

procedure TForm1.ListBox1DblClick(Sender: TObject);

var

aStr: string;

begin

aStr := ListBox1.Items[ListBox1.ItemIndex];

Form2.SetClickedData(DataSource1, Table1, aStr);

Form2.ShowModal;

end;

 

 

unit SndForm;

 

interface

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

DBCtrls, DB, DBTables, StdCtrls;

 

 

private

aDataSource: TDataSource;

aTable: TTable;

aNameStr: string;

public

procedure SetClickedData(aSource: TDataSource; aTbl: TTable;

const aName: string);

end;

 

procedure TForm2.SetClickedData(aSource: TDataSource; aTbl: TTable;

const aName: string);

begin

aDataSource := aSource;

aTable := aTbl;

aNameStr := aName;

end;

 

 

procedure TForm2.FormActivate(Sender: TObject);

begin

DBText1.DataSource := aDataSource;

DBText2.DataSource := aDataSource;

DBText3.DataSource := aDataSource;

DBText4.DataSource := aDataSource;

DBImage1.DataSource := aDataSource;

if aTable.FindKey([aNameStr]) then

begin

DBText1.DataField := 'NAME';

DBText2.DataField := 'SIZE';

DBText3.DataField := 'WEIGHT';

DBText4.DataField := 'AREA';

DBImage1.DataField := 'BMP';

end;

end;