//接口申明
type
ITestObj = interface
['{275C452D-5A6B-49B5-9527-C5D1F582A378}']
function Gettestid: Integer; stdcall;
function Gettestname: string; stdcall;
procedure Settestid(const Value: Integer); stdcall;
procedure Settestname(const Value: string); stdcall;
function testhello: string; stdcall;
property testid: Integer read Gettestid write Settestid;
property testname: string read Gettestname write Settestname;
end; ITestObjs = interface
['{3E517EF1-944D-43F3-AC69-390396977B71}']
function ByID(nID: Integer): ITestObj; stdcall;
procedure Reset;
end; IApps = interface
['{9C4994E3-B41A-47C6-AD8E-8A2AA23EC3E8}']
function TestObjs: ITestObjs; stdcall;
end;var
TestInter: IApps;//接口实现
uses
utestInterface, Classes, SysUtils;type
TTestObj = class(TInterfacedObject, ITestObj)
private
FID: Integer;
FName: string;
function Gettestid: Integer; stdcall;
function Gettestname: string; stdcall;
procedure Settestid(const Value: Integer); stdcall;
procedure Settestname(const Value: string); stdcall;
public
constructor Create;
destructor Destroy; override;
function testhello: string; stdcall;
end; TTestObjs = class(TInterfacedObject, ITestObjs)
private
List: TList;
public
constructor Create;
destructor Destroy; override;
function ByID(nID: Integer): ITestObj; stdcall;
procedure Clear;
procedure Reset;
end; TApps = class(TInterfacedObject, IApps)
private
_ITestObjs: ITestObjs;
public
function TestObjs: ITestObjs; stdcall;
end;//具体实现方法
function TTestObjs.ByID(nID: Integer): ITestObj;
var
I: Integer;
TestObj: TTestObj;
begin
Result := nil;
if not Assigned(List) then
Exit;
for I := 0 to List.Count - 1 do
begin
TestObj := TTestObj(List[I]);
if Assigned(TestObj) and (TestObj.Gettestid = nID) then
begin
Result := TestObj;
Break;
end;
end;
// TODO -Eternally: TTestObjs.ByID default body inserted
end;procedure TTestObjs.Reset;
var
I: Integer;
TestObj: TTestObj;
begin
if not Assigned(List) then
List := TList.Create;
Clear;
for I := 1 to 5 do
begin
TestObj := TTestObj.Create;
TestObj.Settestid(I);
TestObj.Settestname('TestObj' + IntToStr(I));
List.Add(TestObj);
end;
// TODO -Eternally: TTestObjs.Reset default body inserted
end;//------------------------------------------------------------------------------
// 总接口入口
//------------------------------------------------------------------------------
function _WZInterface: IApps;var
_TestObjs: TTestObjs;implementationvar
_testClasses: TApps;//调用
procedure TForm1.Button1Click(Sender: TObject);
var
nID: Integer;
Str: string;
begin
nID := 2;
Str := TestInter.TestObjs.ByID(nID).testname;
ShowMessage(Str);
//调用完这个按钮事件后 ByID出来的接口会被调用TTestObj的Destroy释放,为何会调用Destroy释放?没有手动释放的情况下
// TODO -Eternally: TForm1.Button1Click default body inserted
end;/调用完这个按钮事件后 ByID出来的接口会被调用TTestObj的Destroy释放,为何会调用Destroy释放?没有手动释放的情况下
解决方案 »
- 救急啊,请问下面过程中的:photo是什么意思
- 一个比较奇怪的问题(No Debug Info)
- Internet Transfer Control控件在Delphi中的使用
- 急需解决问题(解决了立马给分100)
- 请大家帮忙,学习C/S结构的方法,最好能给我一个简单的例子。
- 我想把后台的多条记录显示于前台,并能让用户修改部分数据,每条记录后面想附上一个象CHECKBOX之类的小东西,让用户同时提交多条选中的记
- 一个简单问题
- 关于文本文件的读写
- 问各位一个很菜的问题啊,望各位赐教
- 请问在一般数据库开发中,存储过程和触发器有什么用?
- Delphi下载版本求解!!!
- 如果根据句柄获取该程序的窗体边框的高度和宽度?
当在ByID调用时,接口引用机制开始时+1, 事件处理结束后-1, 结果为0
所以,对应的对象就销毁了。正确的做法是: 应该在List中保留接口引用,也就是对象Create出来后
马上只使用它的接口类型的变量进行处理