Time of Update: 2018-12-07
//顯示表單中所有控制項的函數function GetCtrls(Control: TWinControl; List: TStringList): Boolean;var i: Integer; obj: TWinControl;begin for i := 0 to Control.ControlCount-1 do begin obj := TWinControl(Control.Controls[i]); List.Add(obj.Name);
Time of Update: 2018-12-07
//標準文法 TMyClass1 = class(TObject) end; //如果是繼承自 TObject 可以省略 TMyClass2 = class end; //可以實現多個介面; 實現介面時經常用到 TInterfacedObject 類, 它實現了介面的預設方法 TMyClass3 = class(TInterfacedObject, Interface1, Interface2) end; //現在 TMyClass4 相當於 TObject 的別名
Time of Update: 2018-12-07
//例1: 這是正確的 TClassA = class Field1: string; Field2: Integer; end; TClassB = class Field1: string; Field2: Integer; Field3: TClassA; {欄位 Field3 的類型是剛剛新定義的 TClassA 類型} end;//例2: 這是錯誤的, 這裡有個順序問題 TClassA = class Field1: string;
Time of Update: 2018-12-07
類中包含欄位、方法和屬性(屬性包含事件); 欄位是靠方法組織與操作的; 屬性也只是方便和規範了欄位與方法的使用.因此我覺得: 方法是最重要的.方法無處不在, 它不僅僅存在與類中. 先從非類中的方法談起吧, 因為類中的方法也擁有全部這些特性.離開類的懷抱, 我們更喜歡把方法叫做過程或函數.//要點1: 過程用 procedure 定義, 函數用 function 定義; 過程沒有傳回值, 函數需要傳回值.procedure MyProc;begin
Time of Update: 2018-12-07
先建立一個 VCL Forms Application 工程, 代碼中就已經出現了兩個類:一個是 TForm 類; 一個是 TForm1 類; TForm1 繼承於 TForm.TForm 是 TForm1 的父類; TForm1 是 TForm 的子類.unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
Time of Update: 2018-12-07
//要點11: 參數可以分為: 預設參數(傳值)、var(傳址)、out(輸出)、const(常數)四類{預設參數是傳值, 不會被改變}function MyF1(x: Integer): Integer;begin Inc(x); Result := x;end;{var參數是傳址, 會被改變}function MyF2(var x: Integer): Integer;begin Inc(x); Result := x;end;{out參數是為支援Com的, 和 var
Time of Update: 2018-12-07
//這個類中的兩個欄位沒有封裝 TMyClass1 = class FName: string; FAge: Integer; end; //這個類中的兩個欄位封裝了, 外部不能讀寫 TMyClass2 = class private FName: string; FAge: Integer; //public end; //那怎麼讀寫? 用屬性啊 TMyClass3 = class private FName:
Time of Update: 2018-12-07
//要點12: implementation 區中的過程或函數, 只能在本單元調用unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender:
Time of Update: 2018-12-07
//要點18: 如果函數在介面區定義了, 就無需用 forward 提前聲明了unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject)
Time of Update: 2018-12-07
什麼是多態? 我的理解就是: 同樣一個方法, 在不同的對象裡會有不同的實現, 僅此而已.unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3:
Time of Update: 2018-12-07
//要點13: 需要給其他單元調用, 必須在 interface 聲明, 但必須在 uses 區後面unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure
Time of Update: 2018-12-07
//靜態數組做參數, 不能這樣使用:function MyFun(arr: array[0..9] of Integer): Integer;var i: Integer;begin Result := 0; for i in arr do Result := Result + i;end;//應該先把數組定義成一個類型Type IntArray = array[0..9] of Integer; {先把需要的數組定義成一個類型} //給一個靜態數組求和的函數function
Time of Update: 2018-12-07
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure
Time of Update: 2018-12-07
//要點14: 如果聲明在 TForm1 類內, 那它就是 TForm1 類的一個方法了unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender:
Time of Update: 2018-12-07
{現在這個函數並沒有 var 首碼, 也就是說參數應該不會被修改的} function MyFun(p: PInteger): Integer; {PInteger 是 Integer 的指標類型}begin p^ := p^ * 2; Result := p^;end;{測試}procedure TForm1.Button1Click(Sender: TObject);var i,x: Integer;begin i := 8; x := MyFun(@i);
Time of Update: 2018-12-07
//要點15: 調用其他單元的函數//包含函數的單元:unit Unit2;interfacefunction MyFun(x,y: Integer): Integer; {函數必須在介面區聲明}implementationfunction MyFun(x,y: Integer): Integer; {函數必須在函數區實現}begin Result := x + y;end;end.//調用函數的單元:unit Unit1;interfaceuses Windows, Messages,
Time of Update: 2018-12-07
{ 下面的函數重名, 但參數不一樣, 此類情況必須加 overload 指示字; 調用時, 會根據參數的類型和個數來決定調用哪一個; 這就是重載.}function MyFun(s: string): string; overload;begin Result := '參數是一個字串: ' + s;end;function MyFun(i: Integer): string; overload;begin Result := '參數是一個整數: ' + IntToStr(i);end;
Time of Update: 2018-12-07
//弄明白這一點, 才好使用回呼函數{定義方法類型}type TFunType = function(x: Integer): Integer; {函數類型} TProcType = procedure(name: string); {過程類型}{定義一個符合 TFunType 類型的函數}function MyFun(x: Integer): Integer;begin Result := x * 2;end;{定義一個符合 TProcType
Time of Update: 2018-12-07
//把一個方法當作另一個方法的參數, 就是回調方法, 大家習慣稱作回呼函數type TFunType = function(i: Integer): Integer; {聲明一個方法類型}function MyFun(i: Integer): Integer; {建立類型相容的函數}begin Result := i*2;end;{把函數當作參數, 再定義一個函數}function MyTest(x: Integer; F: TFunType): Integer;begin
Time of Update: 2018-12-07
{給這個函數可以賦常數變數}function Fun1(x,y: Integer): Integer;begin Result := x + y;end;{這個函數不能賦予常數變數}function Fun2(var x,y: Integer): Integer;begin Result := x + y;end;{測試}procedure TForm1.Button1Click(Sender: TObject);var i,a,b: Integer;const j = 6; k =