先新建一個 VCL Forms Application 工程, 代碼中就已經出現了兩個類:
一個是 TForm 類; 一個是 TForm1 類; TForm1 繼承於 TForm.
TForm 是 TForm1 的父類; TForm1 是 TForm 的子類.
unit Unit1;
//下面自定義了兩個類, TChild 繼承於 TBass
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
//測試1: 父類只有一個方法 msg1
TBass = class
procedure msg1;
end;
TChild = class(TBass)
procedure msg2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TBass }
procedure TBass.msg1;
begin
ShowMessage('is Bass');
end;
{ TChild }
procedure TChild.msg2;
begin
ShowMessage('is Child');
end;
//測試2: 子類有兩個方法, 自己一個 msg2, 還繼承了一個 msg1
procedure TForm1.Button1Click(Sender: TObject);
var
b: TBass;
begin
b := TBass.Create;
b.msg1; {is Bass}
b.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
c: TChild;
begin
c := TChild.Create;
c.msg1; {is Bass}
c.msg2; {is Child}
c.Free;
end;
end.