在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论