Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; Procedure submit (Sender: tobject ); end; imyinterface = interface procedure proc; end; tmyclass = Class (tinterfacedobject, imyinterface) Public constructor create; destructor destroy; override; Procedure proc; end; var form1: tform1; implementation {$ R *. DFM} constructor tmyclass. create; begin inherited; showmessage ('tmyclass. create '); end; destructor tmyclass. destroy; begin showmessage ('tmyclass. destroy '); inherited; end; Procedure tmyclass. proc; begin showmessage ('imyinterface. proc '); end; Procedure tform1.button1click (Sender: tobject); var C: tmyclass; begin C: = tmyclass. create; C. proc; C. free; showmessage ('************'); {displayed in sequence: tmyclass. create imyinterface. proc tmyclass. destroy **********} end; Procedure tform1.button2click (Sender: tobject); var I: imyinterface; begin I: = tmyclass. create; I. proc; showmessage ('************'); // In this sectionProgram Finally, the compiler can determine that the interface is no longer useful and releases the classes that own it. create imyinterface. proc *********** tmyclass. destroy} end; Procedure tform1.button3click (Sender: tobject); var C: tmyclass; I: imyinterface; begin C: = tmyclass. create; I: = C; // I: = imyinterface (c); {You can also convert it like this} // I: = C as imyinterface; {you cannot use as for the moment, the as conversion can be used only after the interface has a guid} I. proc; showmessage ('************'); {It will be displayed in sequence: tmyclass. create imyinterface. proc *********** tmyclass. destroy} end; Procedure tform1.button4click (Sender: tobject); var I: imyinterface; begin I: = tmyclass. create; I. proc; I: = nil; // You can actively release the interface in this way. At the same time, the showmessage ('***********') will be released for the class that owns it ('**********'); {tmyclass. create imyinterface. proc tmyclass. destroy ***********} end; end.