RE: Inerface and Class reference
"Marcelo Mrack" <[email protected]> Mon, 18 Aug 2003 10:43:38 -0300
| Newsgroups | gmane.comp.lang.delphi.programming |
|---|---|
| Message-ID | <[email protected]> |
1) An interface is like a model for one class. Szyperky define a interface like a contract between two parts, where the first (the interface) defines rules (methods and properties) that another (the class) must realize (in other words, "implements"). See example1 and example2 below:
//EXAMPLE 1 - It´s compiles
type
IMyInterface1 = interface(IInterface)
procedure A;
function B : string;
procedure setX(value : string);
function getX : string;
property X : string read getX write setX;
end;
IMyInterface2 = interface(IInterface)
procedure C;
end;
TMyClass1 = class(TObject,IMyInterface1,IMyInterface2) //Note that TMyClass1 extends only one class - TObject - but implements zero, one or various interfaces - TMyInterface1, TMyInterface2...
private
FX : string;
procedure setX(value : string);
function getX : string;
public
procedure A;
function B : string;
procedure C;
published
property X : string read getX write setX;
end;
implementation
TMyClass1.setX(value : string);
begin
FX := value;
end;
TMyClass1.getX : string;
Result := X;
end;
TMyClass1.A
begin
//...
end;
TMyClass1.C
begin
//...
end;
TMyClass1.C
begin
//...
end;
//EXAMPLE 2 - It´s not compiles. Why not? Because the procedure "A" is not implemented in TMyClass1. In other words, the class not realize the contract between the parts!
type
IMyInterface1 = interface(IInterface)
procedure A;
function B : string;
procedure setX(value : string);
function getX : string;
property X : string read getX write setX;
end;
IMyInterface2 = interface(IInterface)
procedure C;
end;
TMyClass1 = class(TObject,IMyInterface1,IMyInterface2) //Note that TMyClass1 extends only one class - TObject - but implements zero, one or various interfaces - TMyInterface1, TMyInterface2...
private
FX : string;
procedure setX(value : string);
function getX : string;
public
//procedure A;
function B : string;
procedure C;
published
property X : string read getX write setX;
end;
implementation
TMyClass1.setX(value : string);
begin
FX := value;
end;
TMyClass1.getX : string;
Result := X;
end;
{TMyClass1.A
begin
//...
end;}
TMyClass1.C
begin
//...
end;
TMyClass1.C
begin
//...
end;
Interfaces are used to acoplish components (various class). The example exInterfacedForms.zip in http://www.rocket.via.com.br <http://www.rocket.via.com.br/> show a brief use of interfaces.
PS. Excuse me the english, i am braziliam. :-)
-----Original Message-----
From: Allan Smith [mailto:[email protected]]
Sent: segunda-feira, 18 de agosto de 2003 07:04
To: [email protected]
Subject: Re: [Delphi] Inerface and Class reference
On Mon, 18 Aug 2003 07:25:43 -0000, ohm_prasadam wrote:
>Sir/Madam?
>1. What is meant by Interface? why can't we define COM calsses etc. in
>a somple Unit?
>
>2. What is class reference? "TTestClass = class of TSampleClass" What
>is the advantage of declaring like this?
>
>If anybody can answer this please do it.
2) you can assign the class of any descendant class if TSampleClass to
TTestClass and subsequently use it do create an instance of a class which is a
descendant of that class eg if you define TField_Class as Class of TField then
you can assign any descendant of TField to a TField_Class variable anfd use it
to create an instance of that class at run time and the creating procedure
does not need to know what type of field it is creating.
--
Regards,
Allan Smith (Northwood, Middlesex, England)
Yahoo! Groups Sponsor
<http://rd.yahoo.com/M=259395.3614674.4902533.1261774/D=egroupweb/S=1705115362:HM/A=1524963/R=0/SIG=12o885gmo/*http://hits.411web.com/cgi-bin/autoredir?camp=556&lineid=3614674&prop=egroupweb&pos=HM>
<http://us.adserver.yahoo.com/l?M=259395.3614674.4902533.1261774/D=egroupmail/S=:HM/A=1524963/rand=686476079>
Delphi VCL Top Sites!
http://www.sandbrooksoftware.com/TS/TS2/Components.shtml
---------------------------------------------------------------
Unsubscribe:[email protected]
List owner:[email protected]
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service <http://docs.yahoo.com/info/terms/> .
____________________
NOTIFICACAO:
Este email passou pelo controle de conteudo e esta livre de virus.
Setor de Informatica - UNISC
[Non-text portions of this message have been removed]
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/i7folB/TM
---------------------------------------------------------------------~->
Delphi VCL Top Sites!
http://www.sandbrooksoftware.com/TS/TS2/Components.shtml
---------------------------------------------------------------
Unsubscribe:[email protected]
List owner:[email protected]
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/