Virtual method usage

Source: Internet
Author: User

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 = Class (tform)
Button1: tbutton;
Button2: tbutton;
Button3: tbutton;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Procedure button3click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

Tfather = Class
Public
Procedure fa; // virtual;
End;

Tchild = Class (tfather)
Public
Procedure fa; // override;
Procedure CA;
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}

{Tchild}

Procedure tchild. CA;
Begin
Showmessage ('childer ');
End;

Procedure tchild. Fa;
Begin
// Inherited;
End;

{Tfather}

Procedure tfather. Fa;
Begin
Showmessage ('father ');
End;

Procedure tform1.button1click (Sender: tobject); // C-> F
VaR
F: tfather;
Begin
F: = tchild. Create;
F. Fa;
// F. CA; // compilation failed
End;

Procedure tform1.button2click (Sender: tobject); // C (f)
VaR
F: tfather;
Begin
F: = tfather. Create;
Tchild (f). Fa; // *** when declaring a method, if the keyword virtual or override is not required
// *** You must manually call inherited to correctly call the parent class during subclass implementation.
// *** Class implementation method, but if virtual and override are used,
// *** You can call inherited correctly without calling it manually (that is, when you press Ctrl + c
// *** It will automatically add inherited.

Tchild (f). CA; // although the parent class is created, it can be called after forced conversion (resetting the object range.
// But you are not sure whether an unexpected error will occur.
// If the keyword override is used, the compiler will use the late helper, that is, the running is based on the actual
// Determines whether to call the parent class method or subclass method.
// In other words, if a keyword is used, forced conversion does not work. (Role
// It only detects whether this method exists in the parent class and can be called, but it still calls the subclass.
// Method. If not, it cannot be called)
End;

Procedure tform1.button3click (Sender: tobject); // F (c)
VaR
C: tchild;
Begin
C: = tchild. Create;
Tfather (c). Fa;
// Tfather (c). CA; // compilation failed
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.