zoukankan      html  css  js  c++  java
  • 接口委托实现--通过类的对象

    unit Interfacce; {接口委托实现--通过类的对象}
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
      IMyInterface = interface
        function Sell(const s: string): Boolean;
        function Buy(const s: string): Boolean;
      end;
    
      TMyClass = class(TInterfacedObject, IMyInterface)
      protected
        function Sell(const s: string): Boolean;
        function Buy(const s: string): Boolean;
      end;
    
      TMyClass2 = class(TInterfacedObject, IMyInterface)
      private
    //    FmyClass: TMyClass;
        function GetMyClass: TMyClass;  {采用这种方式可以进行一些过滤操作}
      public
        property myService: TMyClass read GetMyClass implements IMyInterface;
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      myClass2: TMyClass2;
    begin
      try
        myClass2 := TMyClass2.Create;
        myClass2.myService.Sell('Book');
        myClass2.myService.Buy('Book')
      finally
        myClass2.Free;
      end;
    end;
    
    { TMyClass }
    
    function TMyClass.Buy(const s: string): Boolean;
    begin
      ShowMessage('Buy ' + s);
    end;
    
    function TMyClass.Sell(const s: string): Boolean;
    begin
      ShowMessage('Sell ' + s);
    end;
    
    { TMyClass2 }
    
    function TMyClass2.GetMyClass: TMyClass;
    begin
    //
    end;
    
    end.
    

      

  • 相关阅读:
    redis使用watch完成秒杀抢购功能:
    OAUTH协议
    常用mysql命令大全
    版本控制器 (Svn,Git)
    vue axios上传文件实例
    vue-resource 和 axios的区别
    js递归算法1+ 2+3.....100的和
    vue-cli title 里面怎动态显示文字
    Entity Framework With Mysql 之Code First
    关于a标签下的img元素在IE7下不能点击的问题
  • 原文地址:https://www.cnblogs.com/devinblog/p/5201649.html
Copyright © 2011-2022 走看看