Delphi interface (3)-release of interfaces

Source: Internet
Author: User
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.
  
   
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.