[Lazarus] How to cast a Interface to a Object in Lazaeus
Marcos Douglas
md at delfire.net
Thu Mar 12 13:36:39 CET 2015
On Thu, Mar 12, 2015 at 9:28 AM, aradeonas <aradeonas at operamail.com> wrote:
> Here an example for you:
>
> type
>
>
> { IFile }
>
> IFile = interface
> function GetName: string;
> procedure SetName(AValue: string);
> property Name: string read GetName write SetName;
> end;
>
> TFileList=specialize TFPGList<IFile>;
>
>
> { TVideoFile }
>
> TVideoFile = class(TInterfacedPersistent, IFile)
> private
> FName: string;
> FWidth: integer;
> function GetName: string;
> procedure SetName(AValue: string);
> public
> property Name: string read GetName write SetName;
> property Width: integer read FWidth write FWidth;
> end;
> { TAudioFile }
>
> TAudioFile = class(TInterfacedPersistent, IFile)
> private
> FName: string;
> FArtist: string;
> function GetName: string;
> procedure SetName(AValue: string);
> public
> property Name: string read GetName write SetName;
> property Artist: string read FArtist write FArtist;
> end;
>
> var
> Form1: TForm1;
>
> implementation
>
> {$R *.lfm}
>
> { TAudioFile }
>
> function TAudioFile.GetName: string;
> begin
> Result:=FName;
> end;
>
> procedure TAudioFile.SetName(AValue: string);
> begin
> FName:=AValue;
> end;
>
> { TVideoFile }
>
> function TVideoFile.GetName: string;
> begin
> Result:=FName;
> end;
>
> procedure TVideoFile.SetName(AValue: string);
> begin
> FName:=AValue;
> end;
>
>
> { TForm1 }
>
> procedure TForm1.FormCreate(Sender: TObject);
> var
> V1,V2:TVideoFile;
> A1,A2:TAudioFile;
> Fl:TFileList;
> begin
> Fl:=TFileList.Create;
> V1:=TVideoFile.Create;
> V1.Name:='V';
> V1.Width:=100;
> A1:=TAudioFile.Create;
> A1.Name:='A';
> A1.Artist:='Art';
> FL.Add(V1);
> FL.Add(A1);
> V2:=TVideoFile(FL.Items[0]);
> A2:=TAudioFile(Fl.Items[1]);
> ShowMessage(V2.Name);
> ShowMessage(A2.Name);
> end;
>
>
> What you think?
For do that, you do not need interface... but if you still continues
using IFile for others reasons, the better use when we work using
interfaces, is using variables that have the type of these interfaces.
Regards,
Marcos Douglas
More information about the Lazarus
mailing list