Preliminary Exploration of the class of Object Pascal (10)

Source: Internet
Author: User

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.

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.