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.
    

      

  • 相关阅读:
    gauss消元
    POJ1229 域名匹配
    HDU3487 play with chain
    POJ1185 炮兵阵地
    POJ2411
    sgu233 little kings
    树形DP初步-真树1662
    树形DP初步-二叉树1661
    c++——string类用法
    UVa1354 ——天平难题
  • 原文地址:https://www.cnblogs.com/devinblog/p/5201649.html
Copyright © 2011-2022 走看看