//下例來子劉藝的《Delphi面向對象編程思想》
unit Demo;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs,IDemo;
type
TEngine = class(TObject)
private
FCapacity: Integer;
FPower: Integer;
public
constructor create;
procedure start;
procedure stop;
end;
TWheel = class(TObject)
private
FNo: Integer;
FSize: Integer;
FTypeName: string;
procedure check;
public
constructor create(size:Integer;TypeName:string;No:Integer);
end;
TVehicle = class(TInterfacedObject)
protected
FColor: string;
FMake: string;
FTopSpeed: Integer;
FWheel: TWheel;
FWheels: TList;
procedure SlowDown;
procedure SpeedUp;
procedure Start;
procedure Stop;
end;
TBicycle = class(TVehicle,IBicycle)
public
constructor create;
destructor Destory;
procedure ride;
end;
TCar = class(TVehicle,ICar)
protected
FEngine: TEngine;
public
constructor create;
destructor Destory;
procedure drive;
end;
implementation
{
*********************************** TBicycle ***********************************
}
constructor TBicycle.create;
var
i: Integer;
begin
FColor:='白色';
FMake:='永久';
FTopSpeed:=20;
ShowMessage(FColor+FMake+'車,最高時速:'+IntToStr(FTopSpeed));
FWheels:= TList.Create;
for i:=1 to 2 do
FWheels.Add(TWheel.create(21,'B型自行車車輪',i));
end;
destructor TBicycle.Destory;
var
i: Integer;
begin
for i:=1 to 2 do
TWheel(FWheels.Items).Free;
inherited;
end;
procedure TBicycle.ride;
begin
start;
speedUp;
showmessage('自行車車行駛中...');
SlowDown;
Stop;
end;
{
************************************* TCar *************************************
}
constructor TCar.create;
var
i: Integer;
begin
inherited create;
FEngine:=TEngine.create;
FColor:='黑色';
FMake:='紅旗';
FTopSpeed:=200;
ShowMessage(FColor+FMake+'車,最高時速:'+IntToStr(FTopSpeed));
FWheels:= TList.Create;
for i:=1 to 4 do
FWheels.Add(TWheel.create(36,'A型汽車車輪',i));
end;
destructor TCar.Destory;
var
i: Integer;
begin
for i:=1 to 4 do
TWheel(FWheels.Items).Free;
FEngine.Free;
inherited;
end;
procedure TCar.drive;
begin
start;
FEngine.start;
speedUp;
showmessage('汽車行駛中...');
SlowDown;
FEngine.stop;
Stop;
end;
{
*********************************** TEngine ************************************
}
constructor TEngine.create;
begin
Fcapacity:=1500;
Fpower:=130;
end;
procedure TEngine.start;
begin
ShowMessage(inttostr(Fcapacity)+'cc,'+inttostr(Fpower)+'匹馬力的發動機發動了!');
end;
procedure TEngine.stop;
begin
ShowMessage('發動機關閉了');
end;
{
*********************************** TVehicle ***********************************
}
procedure TVehicle.SlowDown;
begin
ShowMessage('正在減速...');
end;
procedure TVehicle.SpeedUp;
begin
ShowMessage('正在加速...');
end;
procedure TVehicle.Start;
begin
ShowMessage('車子開始啟動');
end;
procedure TVehicle.Stop;
begin
ShowMessage('車子停下');
end;
{
************************************ TWheel ************************************
}
constructor TWheel.create(size:Integer;TypeName:string;No:Integer);
begin
FSize:=size;
FTypeName:=TypeName;
FNo:=No;
check;
end;
procedure TWheel.check;
begin
ShowMessage('檢查第'+IntToStr(FNo)+'個車輪。型號:'+FTypeName+' 大小:'+IntToStr(FSize));
end;
end.
//=================================================================================
//Dll 文件,這里要封裝的接口
library DemoSvr;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
ShareMem,SysUtils,Classes,
Demo in 'Demo.PAS',
IDemo in 'IDemo.PAS';
{$R *.res}
function CarObj:ICar;
begin
Result:=TCar.create;
end;
function BicycleObj:IBicycle;
begin
Result:=TBicycle.create;
end;
exports
CarObj,BicycleObj;
end.
//=================================================================================
unit frmDemo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IDemo;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function CarObj:ICar ;external 'DemoSvr.dll';
function BicycleObj:IBicycle ;external 'DemoSvr.dll';
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
var MyCar:ICar;
begin
MyCar:=CarObj;
MyCar.drive;
Mycar:=nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var Bicycle:IBicycle;
begin
Bicycle:=BicycleObj;
Bicycle.ride;
Bicycle:=nil;
end;
end.
//=================================================================================
unit IDemo;
interface
type
ICar = interface (IInterface)
['{ED52E264-6683-11D7-B847-001060806215}']
procedure Drive;
end;
IBicycle = interface (IInterface)
['{ED52E264-6683-11D7-B847-001060806216}']
procedure Ride;
end;
implementation
end.
//=================================================================================
program ObjDemo;
uses
Forms,
frmDemo in 'frmDemo.PAS' {Form1},
IDemo in 'IDemo.PAS';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.