Delphi의 Interface 인터페이스 사용 방법

4982 단어 interface
unit Unit1;

interface

uses

 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type

   //               ,                      ;

    ICar = interface (IInterface)

        ['{ED52E264-6683-11D7-B847-001060806215}']

        procedure drive;

    end;

 

//     1,     TinterfacedObject    ;

    TCar = class(TInterfacedObject,ICar)

    public

        procedure drive;

    end;

//     2,     TinterfacedObject    ;

    THouseCar = class(TInterfacedObject,ICar)

    public

        procedure drive;

    end;

 

 TForm1 = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

 private

    { Private declarations }

 public

    { Public declarations }

    function getCar:ICar; //             ICAR,   DLL   ;

 end;

 

var

 Form1: TForm1;

 

implementation

 

{$R *.dfm}

 

{ TCar }

procedure TCar.drive;

begin

 ShowMessage('TCar.drive');

end;

 

{ THouseCar }

procedure THouseCar.drive;

begin

   ShowMessage('THouseCar.drive');

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

 s:ICar;

begin

 s:= getCar;

 s.drive;

end;

 

function TForm1.getCar:ICar;

begin

   Result:=THouseCar.Create; //// Result:=TCar.Create;

end;

 

end.

좋은 웹페이지 즐겨찾기