Delphi interface 接口 总结

阅读: 评论:0

Delphi interface 接口 总结

Delphi interface 接口 总结

接口的功能类似抽象类, 通常定义一套通用的方法,但是在各实现类中有不同的实现方式。

以下几句来源于网络整理:

(1)接口中只有方法、属性,没有字段。所以接口中的属性存取都必须通过方法。

(2)接口中的方法只有声明,没有实现。实现在类中完成。

(3)接口中的方法没有作用域。都是公开的,但是在类中则放到不同作用域。

(4)接口中的方法没有修饰字。可以认为它们都是抽象的。

(5)不能创建接口实例,要使用接口,必须在类中实现,通过类调用接口的方法。

(6)在类中必须声明和实现接口的所有方法。不像类继承中可以选择。

(7)接口中的方法在类中实现时,可以加virtual/dynamic、abstract修饰,从而在子类中可以实现覆盖。

以上内容在下方代码中一一实现(第七条没弄)

开发中经常遇到某某平台的对接什么的,某某消费机对接什么的,其实大致操作流程是差不多的,发送支付请求,发送查询请求,发送退款请求等, 简而言之,接口定义的一套通用的方法.

界面演示截图:

   

工程文件DelphiInterface.dpr代码:

program DelphiInterface;usesForms,uFrmMain in 'uFrmMain.pas' {FrmMain},uPayInterface in 'intfuPayInterface.pas',uCloudPay_A in 'publicuCloudPay_A.pas',uCloudPay_B in 'publicuCloudPay_B.pas',uPay in 'publicuPay.pas';{$R *.res}beginApplication.Initialize;Application.MainFormOnTaskbar := True;Application.CreateForm(TFrmMain, FrmMain);Application.Run;
end.

接口单元uPayInterface.pas代码:

unit uPayInterface;interfaceusesWindows;type//支付接口  通用的有本地初始化  支付请求  查询请求IPay= interface(IInterface)['{F105EA82-976D-4CC8-9315-F3C43055EACF}']     //Ctrl+ Shift+ G 自动生成procedure init;                                                //初始化function getClassname: string;                                 //获取类名function Pay(const sIn: string; out sOut: string): Integer;    //支付function Query(const sIn: string; out sOut: string): Integer;  //查询end;implementationend.

实现类uCloudPay_A.pas代码:

unit uCloudPay_A;interfaceusesuPayInterface;typeTCloudPay_A= class(TInterfacedObject, IPay)privateprocedure init;                                                //初始化publicconstructor Create;destructor Destroy; override;function getClassname: string;                                 //获取类名 function Pay(const sIn: string; out sOut: string): Integer;    //支付function Query(const sIn: string; out sOut: string): Integer;  //查询end;implementation{ TCloudPay_A }constructor TCloudPay_A.Create;
begininit; //做一些初始化操作 比如参数读取 或 参数动态创建
end;destructor TCloudPay_A.Destroy;
begin//可能有些参数需要释放inherited;
end;function Classname: string;
beginResult:= 'TCloudPay_A';
end;procedure TCloudPay_A.init;
begin//各支付平台有各子的配置参数 可在这里读取本地配置文件
end;function TCloudPay_A.Pay(const sIn: string; out sOut: string): Integer;
begin//Result:= 200;
end;function TCloudPay_A.Query(const sIn: string; out sOut: string): Integer;
begin//Result:= 200;
end;end.

实现类uCloudPay_B.pas代码:

unit uCloudPay_B;interfaceusesuPayInterface;typeTCloudPay_B= class(TInterfacedObject, IPay)privateprocedure init;                                                //初始化publicconstructor Create;destructor Destroy; override;function getClassname: string;                                 //获取类名 function Pay(const sIn: string; out sOut: string): Integer;    //支付function Query(const sIn: string; out sOut: string): Integer;  //查询end;implementation{ TWXPay }constructor TCloudPay_B.Create;
begininit; //做一些初始化操作 比如参数读取 或 参数动态创建
end;destructor TCloudPay_B.Destroy;
begin//可能有些参数需要释放inherited;
end;function Classname: string;
beginResult:= 'TCloudPay_B';
end;procedure TCloudPay_B.init;
begin//各支付平台有各子的配置参数 可在这里读取本地配置文件
end;function TCloudPay_B.Pay(const sIn: string; out sOut: string): Integer;
begin//Result:= 200;
end;function TCloudPay_B.Query(const sIn: string; out sOut: string): Integer;
begin//Result:= 200;
end;end.

支付单元uPay.pas代码:

unit uPay;interfaceusesSysUtils, uPayInterface, uCloudPay_A, uCloudPay_B;typeTPay = class(TObject)privateIPayObj: IPay;protected//publicconstructor Create(iType: Integer);destructor Destroy; override;function getPayObjClass: string;                                  //获取IPayObj的创建类名function Pay(const cPay: Currency; out sOut: string): Integer;    //支付function Query(const sOrderId: string; out sOut: string): Integer;  //查询end;implementation{ TPay }constructor TPay.Create(iType: Integer);
begin//根据外部传入的平台类型 创建不同的对象case itype of0: //TCloudPay_AbeginIPayObj:= TCloudPay_A.Create;end;1: //TCloudPay_BbeginIPayObj:= TCloudPay_B.Create;end;end;
end;destructor TPay.Destroy;
begin//接口对象用完会自动释放 也可以通过设置nil主动释放if Assigned(IPayObj) thenIPayObj:= nil; //可以这样主动释放接口; 同时拥有它的类也会释放inherited;
end;PayObjClass: string;
beginResult:= Classname;
end;function TPay.Pay(const cPay: Currency; out sOut: string): Integer;
varsIn: string;
begin//sIn:= ...Result:= IPayObj.Pay(sIn, sOut);
end;function TPay.Query(const sOrderId: string; out sOut: string): Integer;
varsIn: string;
begin//sIn:= ...Result:= IPayObj.Query(sIn, sOut);
end;end.

窗体单元uFrmMain.pas代码:

unit uFrmMain;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls, uPayInterface, uPay;typeTFrmMain = class(TForm)RadioGroup1: TRadioGroup;RadioButton1: TRadioButton;RadioButton2: TRadioButton;Button9: TButton;Button10: TButton;Button11: TButton;Button12: TButton;procedure Button9Click(Sender: TObject);procedure Button10Click(Sender: TObject);procedure Button11Click(Sender: TObject);procedure Button12Click(Sender: TObject);private{ Private declarations }public{ Public declarations }FPay_A: TPay;FPay_B: TPay;FPay: TPay;end;varFrmMain: TFrmMain;implementation{$R *.dfm}procedure TFrmMain.Button10Click(Sender: TObject);
varsOut: string;
beginif Assigned(FPay) thenbeginFPay.Pay(0.01, sOut);PayObjClass+ ' Pay');end;
end;procedure TFrmMain.Button11Click(Sender: TObject);
varsOut: string;
beginif Assigned(FPay) thenbeginFPay.Query('100011011020', sOut);PayObjClass+ ' Query');end;
end;procedure TFrmMain.Button12Click(Sender: TObject);
beginif Assigned(FPay) thenFreeAndNil(FPay);
end;procedure TFrmMain.Button9Click(Sender: TObject);
variType: Integer;
beginiType:= 0;if RadioButton2.Checked theniType:= 1;//iType 可以通过本地ini文件配置或其它配置读取  if not Assigned(FPay) thenFPay:= TPay.Create(iType);
end;end.

在uPay.pas中,定义了IPayObj, TPay.Create方法通过传入的参数iType,创建不同的实现类实例, 通过这种方式, 就不用去分别定义TCloudPay_A,TCloudPay_B的对象,就一个IPayObj就可以了。 由此,没有去测试实现多接口,如果测试的话,就不能像这样定义一个统一的IPayObj入口了。

水平有限,了解不深,后续跟进.

本文发布于:2024-02-05 01:08:37,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170720594161671.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:接口   Delphi   interface
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23