Delphi接口

program Demo1;

{
Create Date: 2014-06-29
Author: P.S.M
1.接口Demo1
} {$APPTYPE CONSOLE} uses
SysUtils; {定义接口}
type
ITestInterface = interface
{GUID通过CTRL+G自动产生}
['{15EAD871-2B5E-4F51-A14E-7D518A2371EF}']
procedure Test;
end; {TInterfacedObject 实现了_AddRef, _Release方法可以自动释放对象}
TTest1 = class(TInterfacedObject, ITestInterface)
public
{接口实现}
procedure ITestInterface.Test = GetTest;
{测试接口}
procedure GetTest;
{重载Destroy方法}
destructor Destroy;override;
end; TTest2 = class(TInterfacedObject, ITestInterface)
public
{接口实现}
procedure Test;
{重载Destroy方法}
destructor Destroy;override;
end; { TTest } destructor TTest1.Destroy;
begin
WriteLn('对象1释放了');
sleep(2000);
inherited Destroy;
end; procedure TTest1.GetTest;
begin
WriteLn('接口1');
end; { TTest2 } destructor TTest2.Destroy;
begin
WriteLn('对象2释放了');
sleep(2000);
inherited Destroy;
end; procedure TTest2.Test;
begin
WriteLn('接口2'); end; procedure Output(Obj: ITestInterface);
begin
Obj.Test;
end; var
ITest1, ITest2: ITestInterface;
begin
{接口什么时候释放? 当它的引用计数为0是自动释放,作用域有效在函数体bend end,函数退出时自动减少引用计数 }
try
ITest1 := TTest1.Create;
ITest2 := TTest2.Create;
Output(Itest1);
OutPut(ITest2);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end; end.

  Delphi接口

上一篇:(76)Wangdao.com第十四天_JavaScript 正则表达式对象 RegExp


下一篇:MySQL忘记密码怎么办