zoukankan      html  css  js  c++  java
  • 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.
    

      

  • 相关阅读:
    模板的导入和继承
    图解从 URL 到网页通信原理
    HTTP协议详细介绍
    django的模型层
    LeetCode3-Longest Substring Without Repeating Characters
    LeetCode2-Add Two Numbers
    LeetCode1-Two Sum
    面试干货整理
    VS中使用QT调用R脚本
    MFC DLL 资源模块句柄切换[转]
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3815385.html
Copyright © 2011-2022 走看看