The first example of the operation of the live video:
Http://www.bianceng.net/delphi/201212/657.htm
Code files:
Unit Unit1;
Interface uses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;
Type TFORM1 = Class (Tform) Button1:tbutton;
Button2:tbutton;
Button3:tbutton;
Button4:tbutton;
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Procedure Button4click (Sender:tobject);
End
IMyInterface1 = interface function Func1:integer;
function Func2:integer;
End
IMyInterface2 = interface procedure Proc1;
Procedure Proc2;
End
TMyClass1 = Class (Tinterfacedobject, IMyInterface1, IMyInterface2) public procedure Proc1;
Procedure Proc2;
function Func1:integer;
function Func2:integer;
End
var Form1:tform1;
Implementation {$R *.DFM} {TMyClass1} function TMyClass1.Func1:Integer;
Begin ShowMessage (' imyinterface1.func1 ');
Result: = 0;
End function TMyClass1.Func2:IntegEr
Begin ShowMessage (' Imyinterface1.func2 ');
Result: = 0;
End
Procedure Tmyclass1.proc1;
Begin ShowMessage (' Imyinterface2.proc1 ');
End
Procedure Tmyclass1.proc2;
Begin ShowMessage (' imyinterface2.proc2 ');
End
Procedure Tform1.button1click (Sender:tobject);
var C:tmyclass1;
Begin c: = Tmyclass1.create;
C.FUNC1;
C.FUNC2;
C.proc1;
C.PROC2;
C.free;
End
Procedure Tform1.button2click (Sender:tobject);
var I1:imyinterface1;
Begin I1: = Tmyclass1.create; I1.
FUNC1; I1.
FUNC2;
End
Procedure Tform1.button3click (Sender:tobject);
var I2:imyinterface2;
Begin I2: = Tmyclass1.create; I2.
Proc1; I2.
PROC2;
End
Procedure Tform1.button4click (Sender:tobject);
var C:tmyclass1;
I1:imyinterface1;
I2:imyinterface2;
Begin c: = Tmyclass1.create;
I1: = C; I1.
FUNC1; I1.
FUNC2;
I2: = C; I2.
Proc1; I2.
PROC2; C.free;
{} end;
End.
Example comments (should now be known):
{
1, Interface naming convention I begins as a class starts with T.
2, the interface is inherited from the iinterface; If you inherit from the root interface, you can omit it.
3, interface members can only be methods, properties, no fields.
4, the interface members are open, do not need private, protected, public, published and other access restrictions.
5, because the interface is only declared, no implementation, also can not use the inheritance and overlay related decoration (virtual, dynamic, abstract, override).
6, an interface can inherit from another interface, but can not inherit from multiple interfaces; However, Delphi.NET has supported multiple inheritance of interfaces.
7, a class can implement multiple interfaces: Tmyclass = Class (Parent, interface 1, interface 2, ...) End;
8, but how rich the class that implements the interface, the interface only has its own declared members.
9, the implementation of the interface class generally inherits from the Tinterfacedobject, directly from the TObject inheritance will add some trouble and duplication of work.
10, the interface will be released after use, and at the same time release the class that owns it; This is convenient, but it brings a lot of problems at the same time.
}