先新建一个 VCL Forms Application 工程, 代码中就已经出现了两个类:
一个是 TForm 类; 一个是 TForm1 类; TForm1 继承于 TForm.
TForm 是 TForm1 的父类; TForm1 是 TForm 的子类.
Code
unit Unit1;
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);
private
{ Private declarations }
public
{ Public declarations }
end;
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;
procedure TForm1.Button1Click(Sender: TObject);
var
b: TBass; //父类只有一个方法 Msg1
begin
b := TBass.Create;
b.Msg1;
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.