Delphi - Dudas sobre interfaces...

 
Vista:

Dudas sobre interfaces...

Publicado por Oscar (1 intervención) el 07/09/2005 10:31:43
Hola, tengo unas dudas sobre el uso de interfaces....

Por ahora paso un programilla y me gustaría saber xq falla y luego me gustaría hacer más preguntas...

El programa tiene 2 botones y 2 listas...
pulsando varias veces al primer botón se crearían varios objetos q luego
al pulsar en el segundo botón, los mostraría en las listas usando las interfaces...
Hay 2 interfaces y cada uno se muestra en una de las listas (TListBox).
unit Unit1;

interface

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

type
INotas = interface
['{F5F6273D-A9C9-44C5-A666-A4F607E18374}']
function getValor: Integer;
end;

INotas2 = interface
['{ADDD2B20-BC87-11D9-9718-0050FCAA723B}']
function getValor2: Integer;
end;

TMisNotas = class(TInterfacedObject, INotas, INotas2)
private
numnotas, numnotas2: Integer;
public
constructor Create;
destructor Destroy;
function getValor: Integer;
function getValor2: Integer;
end;

TForm1 = class(TForm)
Boton: TButton;
Lista: TListBox;
Boton2: TButton;
Lista2: TListBox;
procedure BotonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Boton2Click(Sender: TObject);
private
{ Private declarations }
Inota: INotas;
MiLista: TList;
procedure meterObjetoEnLista(interfaz: INotas);
procedure meterObjetoEnLista2(interfaz: INotas2);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor TMisNotas.Create;
begin
inherited Create;

numnotas:= Random(100);

numnotas2:= 100 + Random(100);
end;

destructor TMisNotas.Destroy;
begin


inherited Destroy;
end;

function TMisNotas.getValor: Integer;
begin
Result:= numnotas;
end;

function TMisNotas.getValor2: Integer;
begin
Result:= numnotas2;
end;

procedure TForm1.BotonClick(Sender: TObject);
var
Objeto: TMisNotas;
begin
MiLista.Add(TMisNotas.Create);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
MiLista:= TList.Create;
end;

procedure TForm1.meterObjetoEnLista(interfaz: INotas);
begin
Lista.Items.Add(IntToStr(interfaz.getValor));
end;

procedure TForm1.meterObjetoEnLista2(interfaz: INotas2);
begin
Lista2.Items.Add(IntToStr(interfaz.getValor2));
end;

procedure TForm1.Boton2Click(Sender: TObject);
var
i, cont: Integer;
begin
Lista.Clear;
Lista2.Clear;

cont:= MiLista.Count;

if (cont > 0) then
for i:= 0 to cont - 1 do
begin
meterObjetoEnLista( INotas( TMisNotas( MiLista.Items[i] ) ) );
meterObjetoEnLista2( INotas2( TMisNotas( MiLista.Items[i] ) ) );
end;
end;

end.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder