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.