Understanding Delphi classes (11)-in-depth methods in classes [7]-about inherited

Source: Internet
Author: User
// Inherited is a special command for calling the parent class method; example: Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; {create a parent class, class includes a function fun and a virtual method proc} tbass = Class procedure proc; virtual; function fun (X, Y: integer): string; end; {four subclasses are created, respectively covering the Virtual Methods of the parent class} tchild1 = Class (tbass) Procedure proc; override; end; tchild2 = Class (tbass) Procedure proc; override; end; tchild3 = Class (tbass) Procedure proc; override; end; tchild4 = Class (tbass) Procedure proc; override; end; var form1: tform1; implementation {$ R *. DFM} {tbass} function tbass. fun (X, Y: integer): string; begin result: = inttostr (x + y); {the parent class function returns the sum of two values.} end; Procedure tbass. proc; begin showmessage ('tbass '); {the virtual method of the parent class will pop up information: tbass} end; {tchild1} procedure tchild1.proc; begin inherited proc; {call the proc method} end of the parent class; {tchild2} procedure tchild2.proc; begin inherited; {call the method with the same name as the parent class can omit the method name} showmessage ('tchild2 '); {then pop up your own information box} end; {tchild3} procedure tchild3.proc; begin showmessage ('tchild3 '); {first pop up your own information box} inherited; {call the method with the same name as the parent class} end; {tchild4} procedure tchild4.proc; begin showmessage (inherited fun (); {call the summation function of the parent class} end; {test} procedure tform1.button1click (Sender: tobject); var C1: tchild1; C2: tchild2; C3: tchild3; C4: tchild4; begin C1: = tchild1.create; C2: = tchild2.create; c3: = tchild3.create; C4: = tchild4.create; c1.proc; {explicit: tbass} c2.proc; {First: tbass; then: tchild2} c3.proc; {First: tchild3; again: tbass} c4.proc; {display: 33; 11 + 22 = 33?} c1.free; c2.free; c3.free; c4.free; 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.