Object Pascal(转载)

─基本語法認識

此網頁主要在介紹 Delphi 程式設計之Object Pascal語法的使用,其中包含 Object Pascal 相關程式檔案應用程式專案架構程式單元架構Pascal 基礎語法等簡單介紹。


Object Pascal 是一種高階的、需要編譯的、嚴格檢查資料型態的程式語言,而且同時支援結構化與物件導向這兩種程式設計方式。它的優點包括了程式可讀性高,編譯過程迅速,以及將應用程式分解成多個程式單元,以便達到模組化程式設計的原則。

Object Pascal 相關程式檔案

Object Pascal 相關程式檔案

原始程式檔案

建立程式所需檔案

編譯程式產生檔案

*.pas 程式單元(unit)原始程式檔
*.dpr 程式專案檔
*.dpk 程式package原始程式檔
*.res 程式資源(Resource)檔
*.dfm 程式表格(form)原始檔
*.dof 程式專案選項設定(Project Options)檔
*.dsk 程式桌面設定(Desktop settings)檔
*.dcu 程式單元(unit)編譯檔
*.exe 程式執行(execute)檔
*.dll 動態連結程式庫(dynamic link library)
應用程式專案架構

*.dpr 程式專案檔

program Project1;  //--專案檔檔案名稱 Project1.dpr (編譯後執行檔名稱 Project1.exe)

uses  //--保留字,加入編譯使用的單元模組
  Forms,  //--指定呼叫 forms 單元程式庫
  Unit1 in 'Unit1.pas' {Form1};  //--指定呼叫 Form1 所在的 Unit1 單元程式庫

{$R *.RES}  //--$R 編譯指引,會編譯連結包含後面 &.RES 延伸檔名至專案檔內

begin  //--程式碼的開始保留字
  Application.Initialize;  //--呼叫 Application 物件的方法
  Application.CreateForm(TForm1, Form1);  //--建立 Form1 物件
  Application.Run;  //--應用程式開始執行
end.  //--程式碼的結束保留字


{ 程式撰寫註解可使用 {註解} 或 //註解 或 (*註解*) } 

範例:
program sal2000;

uses
  Forms,
  SysUtils,
  Windows,
  Dialogs,
  sal000 in 'sal000.pas' {sal000f},
  saldm in 'saldm.pas' {dm: TDataModule},
  .
  .
  sal001 in 'sal001.pas' {sal001f};

{$R *.RES}

begin
  Application.Initialize;
  Application.Title := '進銷存管理系統';  
   //建立主視窗前顯示版權視窗
  SplashFrm := TSplashFrm.Create( Application );  
  try
    SplashFrm.Show;
    SplashFrm.Update;
  finally
    SplashFrm.Free;
    if IsLogin('進銷存管理系統', 'SAL', True)= True then begin 
//執行密碼驗證畫面
      Application.CreateForm(Tsal000f, sal000f);
      Application.Run; 
   
end;
  end;
end;

end. //僅供參考

*.pas 程式單元(unit)原始程式檔

unit Unit1;  //--程式單元名稱 Unit1.pas (單元名稱必須是唯一的,編譯後名稱 unit1.dcu)

interface  //--保留字

uses  //--保留字,加入編譯使用的單元模組
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type //--保留字,
宣告新的類別及其成員
  TForm1 = class(TForm)
   Edit1: TEdit;
  private
  { Private declarations }
  (* 私有變數及副程式(函數與程序)宣告處 *)
  public
  { Public declarations }
 
(* 公共變數及副程式(函數與程序)宣告處 *) 
  end;

var //--
宣告物件及資料型態
  Form1: TForm1;

implementation //--實作部份

{$R *.DFM} //--$R 編譯指引,會編譯連結包含後面 &.DFM 延伸檔名至單元檔內

uses unit2;  //--加入使用的程式單元

{程式碼}  //--initilization {初始話區段}

end. //--程式單元結束

特殊符號

#  $  &  '  (  )  *  +  ,  ?  /  :  ;  <  =  >  @  [  ]  ^  {  }
(*  (.  *)  .)  ..  //  :=  <==  >==  <>

識別字

可以是任意長度,但有效長度為255字元。第一字元必須是英文字母[a..z][A..Z]或底線_,中間不得空白,保留字得做為識別字使用。Object Pascal 中英文字母大小寫等同視之。
如:unit2.CurrentValue

保留字

and  Downto  in  or  string  array  else  inherited  out  then  as  end  initialization  packed  threadvar  asm  except  inline  procedure  to  begin  exports  interface  program  try  case  file  is  property  type  class  finalizetion  label  raise  unit  const  for  mod  repeat  uses  destructor  function  nil  resourcestring  var  dispinterface  goto  not  set  while  div  if  object  shl  with  do  implementation  of  shr  xor

變數宣告

var
  
變數名稱 : 資料型態 ;

procedure Example1;
 var 
//--
保留字, 宣告
    A, B, C : Integer;  //--A, B, C 為整數型態
   
{A := 10;  //指定敘述}
begin

end;

資料類別

整數型態 (Integer Type)

型態 範圍 格式
ShortInt -128 ~ 127 Signed 8-bit
Integer -32678 ~ 32767 Signed 16-bit
LongInt -2147483648 ~ 2147483647 Signed 32-bit
Byte     0 ~ 255 Unsigned 8-bit
Word         0 ~ 65535 Unsigned 16-bit

布林型態 (Boolean Type)

型態 大小  
Boolean 1 Byte =0 -> False  ;  =1 -> True  ;
ByteBool 1 Byte =0   -> False  ;
<>0 -> True   ;
LongBool 2 Byte
WordBool 4 Byte

字元型態 (Char Type)

型態 大小  
Char 1 Byte var 
    CharTest : Char ;
    CharTest := 'A' ;

字串型態 (String Type)

型態 大小  
ShortString (短字串) <= 255   Str1 : String ;  // Str1 := 'Hello Delphi' ;
 Str2 : String[50] ;  //
最多20個字元
String (長字串)

實數型態 (Real Type)

型態 範圍 大小 精度
Real 2.9x10 ^-39 ~ 1.7x10^38 6 Byte 11 - 12
Single 1.5x10 ^-45 ~ 3.4x10^38 4 Byte 7 - 8
Double 5.0x10 ^-324 ~ 1.7x10^308 8 Byte 15 - 16
Extented 3.4x10 ^-4932 ~ 1.1x10^4932 10 Byte 19 - 20

陣列型態 (Array Type)

陣列

宣告

 
一維陣列   var 變數名稱:Array[註標] of 資料型態; S : Array[1..4] of String;
二維陣列   var 變數名稱:Array[index1, index2] 
                      of 
資料型態;
N : Array[1..9, 1..9] of Integer;
多維陣列   var 變數名稱:ArrayArray[index1, index2, 
                       index3, ....] of
資料型態;

資料錄型態 (Record Type)

宣告

 
Type
 
資料錄型態:Record
            
常數1:資料型態; 
            
常數2:資料型態; 
             .....
end;

var 
 
變數名稱:資料錄型態; 

Type
  TPerson
Record
       Pnm
String 
       Age 
Integer 
       .....
end;

var
  Cthing
TPerson 
begin
  Cthing.Pnm
= 'AHsien'
 
Cthing.age= 99
end


常數宣告 (Constant)

宣告

 
 const  變數名稱=常數值; const N=3.1416; S='Hello';

運算子

關係運算子
運算子 說明 範例 結果
= 等於 1=2 False
<> 不等於 1<>2 True
< 小於 1<2 True
> 大於 1>2 False
<= 小於等於 1<=2 True
>= 大於等於 1>=2 False

邏輯運算子

運算子 說明 範例  
not not (1+1=3)
and x>0 and x<100
or y<0 or y>100
xor 1<0 xor 90>100 = False 兩運算子

運算子優先順序

運算子 優先順序 範例
NOT,- (負號) 第一 (最高) A<B+3
A<(B+3)
*,/,DIVMODAND 第二
+,-,ORXOR 第三
=,<>,<,>,<=,>= 第四 (最低)

迴圈敘述

for 迴圈敘述
語法 範例

for 控制變動 := 初值 to 終值   //每次加一
do begin
        敘述 ;
        ......(
當敘述執行兩列時, 必須用 begin .. end; 
                    括起來
)
    end ;

var Sum, I : Integer;
begin
    Sum := 0;
    for I := 1 to 100
    do Sum:= Sum + 1;  
end;

for 控制變動 := 初值 downto 終值    //每次減一
do begin
        敘述 ;
        ......(
當敘述執行兩列時, 必須用 begin .. end; 
                    括起來
)
    end ;

var Sum, I : Integer;
begin
    Sum := 0;
    for I := 100 downto 1
    do Sum:= Sum + 1;  
end;

for 巢狀迴圈

for 控制變動 := 初值 to 終值   //第一層
do begin
        for 控制變動 := 初值 to 終值  //第二層
          do
敘述 ;
    end ;

var  I, J : Integer;
begin
     for I := 1 to 9
    do begin
            for J := 1 to 9
            do ListBox1.Items.Add(IntToStr(I)+'
'+
                   IntToStr(J)+'
'+IntToStr(I*J);  
        end;
end;


while .. do 迴圈敘述

語法 範例

while 條件運算式   
do begin
        敘述 ;
        ......(
當敘述執行兩列時, 必須用 begin .. end; 
                    括起來
)
    end ;

var Sum, I : Integer ;
begin
    Sum := 0 ;
    I := 1 ;
    while  I<=100
    do begin
            Sum := Sum + 1 ;
            I := I + 1 ;
         end ;
end;

while .. do 巢狀迴圈敘述   // 同 for 巢狀迴圈


repeat .. until 迴圈敘述

語法 範例

repeat   
   敘述一 ;
    ......(
當敘述執行兩列時, 必須用 begin .. end; 
               括起來
)
until
條件運算式 ;

var Sum, I : Integer ;
begin
    Sum := 0 ;
    I := 1 ;
    repeat
       Sum := Sum + 1 ;
        I := I + 1 ;
    until I>100 ;
end;

repeat .. until 巢狀迴圈敘述   // 同 for 巢狀迴圈


條件敘述

if .. then 條件敘述
語法 範例

if 條件運算式  
then begin
   
        敘述 ;
           ......(
當敘述執行兩列時, 必須用 begin .. end; 
                    括起來
)
        end ;

var Sum, I : Integer;
begin
    Sum := 0;
    I := 1;
    if I > 0 then Sum := Sum + 1;  
end;

if 條件運算式  // 當條件成立,執行敘述A
then begin
   
        敘述A ;
           ......(
當敘述執行兩列時, 必須用 begin .. end; 
                    括起來
)
        end
else begin 
// 當條件不成立,執行敘述B
         
敘述B ;
           ......(
當敘述執行兩列時, 必須用 begin .. end; 
                    括起來
)
       end; 

var I : Integer;
begin
    I := 1;
    if I > 0 
    then Label1.Caption := ' I
大於零 '
    
else Label1.Caption := ' I 小於等於零 ' ;
end;

if .. then 巢狀條件敘述

if 條件運算式一   //第一層
then  begin
           if 條件運算式二  //第二層
              then
敘述 ;
        end ;

var  I : Integer;
begin
    I := 1;
    if I > 0 
    then begin
               if I = 1 
               then Label1.Caption := ' I
等於一 '
                  
else Label1.Caption := ' I 不等於一 ' ;
            end;
end;


case .. of 條件敘述

語法 範例

case 條件判斷變數 of    
   變數選項1 :  敘述1 ;
  
變數選項2 :  敘述2 ;
  
變數選項3 :  敘述3 ;
   .....
  
變數選項n :  敘述n ;    
else //
可省略
    敘述
Z
end ;

var I : Integer ;
begin
    case I of 
        1 : Label1.Caption := '
星期日 ' ;
        2 : Label1.Caption := '
星期一 ' ;
        3 : Label1.Caption := '
星期二 ' ;
        4 : Label1.Caption := '
星期三 ' ;
        5 : Label1.Caption := '
星期四 ' ;
        6 : Label1.Caption := '
星期五 ' ;
        7 : Label1.Caption := '
星期六 ' ;
    else Label1.Caption := '
資料錯誤 ' ;
    end ;
end;

or 

var I : Integer ;
begin
    case I of 
        1,3,5,7,9 : Label1.Caption := '
奇數 ' ;
        0,2,4,6,8 : Label1.Caption := '
偶數 ' ;
    end ;
end;


with 條件敘述

語法 範例

with 欄位變數名稱或物件名稱
do begin   
         敘述 ;
        ......(
當敘述執行兩列時, 必須用 begin .. end; 
               括起來
)
     end;

type TDater = Record
           Day : Integer ;
           Month : Integer ;
           Year : Integer ;
       end;
var DateStr : TDate ;
begin
    with DateStr
    do begin
            Day := 28 ;  
// DateStr.Day := 28 ;
            Month := 2 ; 
// DateStr.Month := 2 ;
            Year := 2001 ; 
// DateStr.Year := 2001 ;
         end;
end;


posted on 2004-07-06 15:04  Eagletian  阅读(203)  评论(0编辑  收藏  举报

导航