Overwrite Method
Let's first discuss the virtual method, or useTairplaneFor example, note thatTakeoffMethod inTairplaneClass is a virtual method (TakeoffMethod declaration endsVirtualKeyword ),TakeoffMethodSendmessageCall and respondMsgtakeoffMessage, if its derived classTmilitaryplaneClass does not provide its ownTakeoffMethod, base classTairplaneOfTakeoffThe method is called. BecauseTmilitaryplaneClass providesTakeoffMethod to callTmilitaryplaneClassTakeoffInstead of calling the method of the base class.
In a derived class, replace the method name of a base classOverwrite Method.
In order to be able to perform overwriting, the features of this method must strictly match the methods in the base class, that is, the return type, method name, and parameter table must be consistent with the base class method. In addition, the methods in the derived class must use keywords.OverrideStatement.
Note
Object Pascal also has a dynamic method. For most programmers, the dynamic method is the same as the virtual method. The difference is that the virtual method pointer is stored in the classVirtual method table(VMT, virtual method table.
It doesn't matter whether you understand the difference at present. Here we just want to let everyone know that there are dynamic methods, so as not to encounter it when looking at the Delphi example and VCL source code. In most cases, the process of dynamic methods in a program is the same as that of virtual methods.
Use the override method instead of the base class method, or use the override method to enhance the base class method. ToTakeoffFor example, if you want to completely replaceTairplaneClassTakeoffMethod to overwrite it. The required code is provided below:
Procedure tmilitaryplane. Takeoff (DIR: integer); begin {New Code} end;
If you want to inherit the functions of the base class and add some functions on this basis, you should first call the base class method and then add new code. Key for calling methods of the base classInheritedFor example:
Procedure tmilitaryplane. Takeoff (DIR: integer); begin {First call the takeoff method of the base class} inherited takeoff (DIR); {New Code} end;
By calling the base class method, you can obtain the original performance of the Methods written in the base class, and then add new code before or after the base class method is called to enhance the base class method. Note:TakeoffMethod declaration inTairplaneClassProtectedSegment.PrivateSegment, it will not work, because even the derived class cannot access the private members of its ancestor class, by settingTakeoffMethod:Protected(Protected) to hide it from the outside, but its derived class can still be accessed.
Note
ForPrivate access(Private) andSecure Access(Protected) The rule has an exception. If the derived class is declared in the same unit of the base class, the private fields and methods of the base class are valid for the derived class, however, if the derived class is declared in an independent unit, only the protected fields and methods of the base class are valid for the derived class.
When using another derived class, you must ensure that the base class constructor is called so that all ancestor classes can be correctly initialized. The key to calling the base class constructor is also required.Inherited, LookTairplaneClass constructor:
Constructor tairplane. create (aname: string; akind: integer); begin {initialization process, set aircraft name, type, and raise limit} inherited create; Name: = aname; kind: = akind; Status: = onramp; Case kind of airliner: Ceiling: = 35000; commuter: Ceiling: = 20000; privatecraft: Ceiling: = 8000; end;
Note thatCreateThe constructor ensures that this class is correctly created. If the base class is not explicitly declared when a class is declared, the base class is automaticallyTobject. In a class constructor, you must call the base class constructor.