zoukankan      html  css  js  c++  java
  • 接口的委托实现(通过接口)

    unit Unit1;
    
    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;
    
      IBaseInterface = interface
        procedure Buy(s : string);
      end;
    
      IMyInterface = interface(IBaseInterface)
        procedure Sell(s: string);
      end;
    
      TBaseClass = class(TInterfacedObject, IMyInterface)
        procedure Buy(s : string);
        procedure Sell(s: string);
      end;
    
      TMyClass = class(TInterfacedObject, IBaseInterface)
      private
        ITest: IBaseInterface;
      protected
        property service: IBaseInterface read ITest implements IBaseInterface;
      public
        constructor Create(AClass: TClass);
        destructor Destory(AClass: TClass);
    
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    { TBaseClass }
    
    procedure TBaseClass.Buy(s: string);
    begin
      ShowMessage('Buy ' + s);
    end;
    
    procedure TBaseClass.Sell(s: string);
    begin
      ShowMessage('Sell ' + s);
    end;
    
    { TMyClass }
    
    constructor TMyClass.Create(AClass: TClass);
    var
      baseObj: IMyInterface;
    begin
      baseObj := TBaseClass(AClass.NewInstance);
      ITest := baseObj;
    end;
    
    destructor TMyClass.Destory(AClass: TClass);
    begin
      ITest := nil;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      test: TMyClass;
    begin
      try
        test := TMyClass.Create(TBaseClass);
        test.service.Buy('aa');
      finally
        test.Free;
      end;
    end;
    
    end.
  • 相关阅读:
    wpf arcgis engine 当前没有或未启用Spatial Analyst许可解决办法
    arcglobe 图层三大类说明
    sql自带函数语句
    wpf 前台获取资源文件路径问题
    Microsoft.Office.Interop.Excel的用法
    WPF:父窗口与子窗口的层次关系
    wpf 拖图片到窗体
    wpf comboBox取值问题
    wpf 窗体内容旋转效果 网摘
    js拖动滑块
  • 原文地址:https://www.cnblogs.com/devinblog/p/5201968.html
Copyright © 2011-2022 走看看