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.
    

      

  • 相关阅读:
    AgilePoint模型驱动BPM(业务流程管理)介绍
    WF从入门到精通(第五章):workflow跟踪 (转)
    昕友.亿达PM项目管理软件 结构草图
    C++之虚拟继承
    Using Batch Parameters
    Static 关键字 C and C++
    something about code coverage planning
    C++ 虚函数表
    C++ FAQ for me
    Drag and Drop in WPF
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3815385.html
Copyright © 2011-2022 走看看