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