//接口申明
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释放?没有手动释放的情况下