Preliminary Exploration of Object Pascal class (9)

Source: Internet
Author: User

Inheritance)

In Object Pascal, one of the most powerful functions of a class is that it can be extended through inheritance.

Inheritance)It refers to getting an existing class and adding functions by deriving a new class from it.

The class for getting started is calledBase Class(Base class) orAncestor class(Ancestor class), the newly created class is calledDerived class(Derived class ).

To illustrate these concepts, let's go backTairplaneClass. As we all know, there is a big difference between the civil and military fields. To express a military aircraftTairplaneAnd add some functions:

Tmilitaryplane = Class (tairplane) {tmilitaryplane inherited from the tairplane base class} private themission: tmission; function getstatus (VAR statusstring: string): integer; override; {override overwrites the getstatus method of the base class} constructor create (aname: string; Atype: integer); protected procedure takeoff (DIR: integer); override; Procedure land; override; Procedure attack; virtual; {attack, setmission is the new method of tmilitaryplane} procedure setmission; virtual; end;

TairplaneAll content,TmilitaryplaneIn addition, it also adds some new content. Note the 1st rows and Keywords of the class declaration.ClassThe class name in the following arc is used to indicate that the Compiler class is derived from another class, and the derived class is the base class. Here, the base class isTairplaneClass.

Note

When a new class is derived from another class, in addition to some new features, the new class inherits all the functions of the base class. You can add fields and methods to the new class, but cannot delete any content provided by the base class.

InPrivateSection, there is a line declaredTmissionClass instance, which does not provide a statement here, but encapsulates every military task to be handled by a military aircraft: aiming, navigation, Import and Export height, course, and so on. This also shows the usage of the Instance field as another class. In fact, you will see a lot in the specific programming in Delphi.

The above code passes the test in Delphi7. Download the sample code:Inheritance (tmilitaryplane).rar

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.