关于Delphi中Interface的作用
来源:互联网 发布:怎么开个淘宝网店进货 编辑:程序博客网 时间:2024/05/17 21:54
个人认为引入Interface的主要作用是有利于软件的升级维护,避免模块的耦合,而不是某些人认为的为了实现多重继承。
以下是一个网上的小例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
//定义接口如果接口独立成一个文件,则在应用程序中和实现程序中都要引用该接口文件;
ICar = interface (IInterface)
['{ED52E264-6683-11D7-B847-001060806215}']
procedure drive;
end;
//接口实现类1,一定要加入TinterfacedObject才能使用;
TCar = class(TInterfacedObject,ICar)
public
procedure drive;
end;
//接口实现类2,一定要加入TinterfacedObject才能使用;
THouseCar = class(TInterfacedObject,ICar)
public
procedure drive;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function getCar:ICar; //此处可用别的方式来实现获取ICAR,如放在DLL文件中;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TCar }
procedure TCar.drive;
begin
ShowMessage('TCar.drive');
end;
{ THouseCar }
procedure THouseCar.drive;
begin
ShowMessage('THouseCar.drive');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s:ICar;
begin
s:= getCar;
s.drive;
end;
function TForm1.getCar:ICar;
begin
Result:=THouseCar.Create; //此处为整个实现的关键点;
//或Result:=TCar.Create;
end;
- 关于Delphi中Interface的作用
- JAVA中interface的作用
- Delphi中Interface接口的使用方法
- Delphi中Interface接口的使用方
- Delphi中Interface接口的使用方法
- Delphi中 “@” “^” 符号的作用
- .m中@interface部分的作用
- interface的作用
- interface接口的作用
- iOS中.m中,@interface部分的作用
- 关于java中 abstract interface 的说明
- 关于Interface中定义变量的注意事项
- delphi中application.processmessages的作用
- Delphi 中initialization和finalization的作用
- delphi中application.processmessages的作用
- delphi中application.processmessages的作用
- objective-c中interface与protocol的作用
- objective-c中interface与protocol的作用
- (转载)Google百度和谷歌的那些事(令人击赏)
- [技术学习]博客客户端介绍(转载)
- 鼠标滑过图片出现大图片提示层
- OpenGL的几个库
- 深入理解sizeof
- 关于Delphi中Interface的作用
- 《程序员的创世传说》第一节 传说中的1978年
- .Net Framework中的委托与事件
- Qt 4.5授权说明
- JS数组方法汇总 array数组元素的添加和删除
- 易安故词
- 课程设计
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- How can I change the TCP/IP tuning parameters?