使用dll进行对象的封装(1)
来源:互联网 发布:苹果手机虚拟定位软件 编辑:程序博客网 时间:2024/06/07 00:45
使用dll进行对象的封装:
使用Dll来封装对象。主要限制在于:。调用dll的应用程序只能使用dll中对象的动态绑定的方法。这点是必须的。。dll封装的对象只能在dll中进行创建。。在dll和调用dll的应用程序中都需要对封装的对象及其被调用的方法进行声明。实现dll中对象的动态绑定的方法可以使用virtual和接口两种方法来进行。使用抽象类也可以是实现dll中对象的动态绑定。
1.简单demo演示dll进行类对象的封装。
a.dll
unit untobj;interfacetype TMan = class// 类的实现 private FAge: Integer; public function GetAge: Integer; virtual; //使用后期绑定的方法。 constructor Create; end;implementation{ TMan }constructor TMan.Create;begin FAge := 13;end;function TMan.GetAge: Integer;begin Result := FAge;end;end.uses SysUtils, Classes, untobj in 'untobj.pas';// 主要限制在于://。调用dll的应用程序只能使用dll中对象的动态绑定的方法。这点是必须的。//。dll封装的对象只能在dll中进行创建。//。在dll和调用dll的应用程序中都需要对封装的对象及其被调用的方法进行声明。{$R *.res}function ManObj: TMan; //在dll中到处类对象。begin Result := TMan.Create;end; exports manobj;beginend.
2.调用dll的实现的过程。
unit untDemo;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMan = class //在调用dll的程序中进行相应类的生明。 public function GetAge: Integer; virtual;abstract; // 使用抽象的方法来进行声明。 end;type TForm1 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; function ManObj: TMan; external 'dlldemo.dll'; //引用dll中的函数。implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);var man: TMan; i: Integer;begin man := ManObj; // 调用程序中使用dll导出的对象。 if man = nil then Exit; try i := man.GetAge; ShowMessage(IntToStr(i)); finally man.Free; end; end;end.第二:使用接口来实现类的封装的简单示例:1.dll中类以及接口的声明和实现。在调用dll程序中的unit untISay;// 接口的声明。interfacetype ISay = interface ['{BB0B17F4-92FB-4B10-83BF-89300D58B8BA}'] procedure say; function sayInteger: Integer; end;implementationend.--------------------------------------------------------------------------------------------// 类的实现。unit untMan;interfaceuses Dialogs,untISay;type TMan = class(TInterfacedObject,ISay) procedure Say; function SayInteger:Integer; end;implementation{ TMan }procedure TMan.Say;begin ShowMessage('nihaoma');end;function TMan.SayInteger: Integer;begin Result := 100;end;end.--------------------------------------------------------------------------dll中导出相应的接口的函数。uses SysUtils, Classes, untISay in 'untISay.pas', untMan in 'untMan.pas';{$R *.res}function outputobj: ISay;begin Result := TMan.create;// 这个关键的地方。end; exports outputobj;beginend.
unit untmain;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,untISay, StdCtrls;// 主要添加相应的UntISay接口声明的单元type TForm1 = class(TForm) btn1: TButton; btn2: TButton; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); private { Private declarations } public { Public declarations } m: ISay;// end;var Form1: TForm1; function outputobj:ISay; external 'dllobj.dll'; //----------------------------implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);begin m := outputobj; m.say; m := nil;end;procedure TForm1.btn2Click(Sender: TObject);begin m := outputobj; ShowMessage(IntToStr(m.sayInteger)); m := nil;end;end.
第三。使用抽象类的情况。
- 使用dll进行对象的封装(1)
- Spring中封装将对象使用注解方式进行封装
- IOS对象关系映射(ORM)之coreData框架的学习——(三)对使用步骤进行方法封装
- 使用DLL文件中封装的窗口
- 使用DLL文件中封装的窗口
- 使用DLL封装应用程序的资源
- 使用DLL文件中封装的窗口
- 使用RedisDB,封装dll
- 使用VBS对FSO对象进行类封装
- 使用Hibernate进行对象的关系映射(1)
- java使用fastjson进行数据的封装
- 对象1(封装)
- 对面向对象的特点之一的封装进行理解
- C#使用vs2010封装DLL
- 将Unity的脚本封装为dll文件(使用monodevelop编译器)
- 将Unity的脚本封装为dll文件(使用monodevelop编译器)
- 使用Swig封装的python对象
- 使用BeanUtil封装带日期的对象
- jsp登陆模块
- J2EE到底是什么?
- 参数数组
- String.Empty,NULL和""的区别
- 从Salesforce到Facebook再从iPod+iTunes到Blackberry看电信重组
- 使用dll进行对象的封装(1)
- 不出十年,纯粹意义上的软件开发即将死亡
- Jawe一二
- GDB概述
- 论毕业设计的倒掉
- Jawe
- Jawe
- Jawe
- Jawe