unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
THuman=class //基类
public
procedure showinfo1;virtual;
procedure showinfo2;virtual;
end;
TStudent=class(Thuman) //派生类
public
procedure showinfo1;override;// 虚拟继承
procedure showinfo2;override;
end;
TWorker=Class(THuman) //派生类
public
procedure showinfo1;override;
procedure showinfo2;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure THuman.showinfo1;
begin
ShowMessage('THuman Showinfo1');
end;
procedure THuman.showinfo2;
begin
ShowMessage('THuman Showinfo2');
end;
procedure TStudent.showinfo1;
begin
ShowMessage('TStudent Showinfo1');
inherited showinfo2; //调用父类中不同名的成员函数
end;
procedure TStudent.showinfo2;
begin
ShowMessage('TStudent Showinfo2');
end;
procedure TWorker.showinfo1;
begin
ShowMessage('TWorker Showinfo1');
inherited; //调用父类中的同名函数
end;
procedure TWorker.showinfo2;
begin
ShowMessage('TWorker Showinfo2');
end;
procedure TForm1.btn1Click(Sender: TObject);
var
H:THuman;
begin
H:=THuman.Create;
h.showinfo1;
h.showinfo2;
end;
//运行结果'THuman Showinfo1'
//然后'THuman Showinfo2'
procedure TForm1.btn2Click(Sender: TObject);
var
S:TStudent;
begin
s:=TStudent.Create;
s.showinfo1;
s.showinfo2;
end;
//运行结果'TStudent Showinfo1'
//然后'THuman Showinfo2'
//然后'TStudent Showinfo2'
procedure TForm1.btn3Click(Sender: TObject);
var
W:TWorker;
begin
w:=Tworker.Create;
w.showinfo1;
w.showinfo2;
end;
//运行结果'TWorker Showinfo1'
//然后'THuman Showinfo1'
//'TWorker Showinfo2'
end.