Talking about polymorphism - concept description

zhaozj2021-02-08  260

On the polymorphism - concept description 2001.9.25 Author: Nicrosoft (Chennai soft nicrosoft@sunistudio.com) Personal Home Page: http: //www.sunistudio.com/nicrosoft/ East Documents: http: //www.sunistudio. COM / ASP / SUNIDOC.ASP

Polymorphism, this core concept of object-oriented programming, it is unlikely, it is unlikely to be clearly understood in one text. In addition, the author is still constantly learning, and the level is limited. Therefore, this article can only describe the polymorphic contour, enabling the reader to understand. If you have a place where you are not allowed, welcome to point out, or discuss with the author (author email: nicrosoft@sunistudio.com) First, what is polymorphisn? Pressing the literal meaning is "multiple shapes". I didn't find a description of a polymorphic theoretical concept on my hand. Temporary a plot of CHARLIE CALVERTS - Polymorphism is to allow you to set the parent object to become a technology equal to one or more of his child objects, the parent object can be assigned according to the current assignment Its child objects operate in different ways (taken from "Delphi4 programming technology insider"). Simply put, it is a sentence: allowing a pointer to the child class type to the parent class type. Polymorphisms are implemented through virtual function in Object Pascal and C . Ok, then "virtual function" (or "virtual method"). The virtual function is to allow the member function that is redefined by its subclock. Subclasses redefine the parent class virtual function, called "override", or "rewriting".

Here is an initiator often confused concepts. Override and overload. The above is said that the overlay is the method of defining the virtual function of the parent class. And overloading means that there are multiple symbols that are allowed to exist, and the parameter tables of these functions are different (may although the number of parameters is different, the parameter type is different, maybe both different). In fact, the concept of overload is not "object-oriented programming". The reimbursement is true: the compiler is modified according to the parameter table different from the function, and then the same name function has become different functions (at least This is like this for the compiler). For example, there are two same name functions: Function func (p: integer): Integer; and Function func (p: string): integer; Then then the compiler has been modified, the name may be like this: int_func, str_func. For the call of these two functions, it has been determined between the compiler, is static (remember: is static). That is, their address is bound in the compile period (early binding), so the overload and polymorphism have nothing to do! Really and polymorphisms are "overlay". When the subclass redefines the virtual function of the parent class, the parent class pointer dynamically (remember: is a dynamic!) Of the number of the subclass of the function, such a function call is called by the different subclass pointer assigned to it. During the compilation period is unable to determine (the address of the virtual function of the subclass of the subclass cannot be given). Therefore, such a function address is binding in the run period (late state). The conclusion is that overload is only a language characteristic, and has nothing to do with polymorphism, regardless of object-oriented! Quote Bruce Eckel: "Don't make it silk, if it is not a late state, it is not a polymorphism." So, what is the role of polymorphism? We know that the package can hide the implementation details, making the code modular; inherit can extend the existing code module (class); their purpose is for-code reuse. And the polymorphism is to achieve another purpose - the interface is reused! And the reality is often, it is difficult to reuse the code, and the true most valuable reuse is the interface reuse, because the interface is the company's most valuable resource. Design interface is more time for this interface than using a pile of classes. And The interface requires more expensive manual time. "In fact, the reasons for inheriting the reuse of the code have become weaker, because" combination "can replace the function of the inheritance, and" combine " The performance is better (at least "type explosion" can be prevented). Therefore, the author believes that the existence of inheritance is largely as a "polymorphism" basis rather than extended existing code.

What is the interface reuse? We will give a simple example, suppose we have a base class that describes the aircraft (Object Pascal language description, the same): type plane = class public procedure fly (); virtual; abstract; // Take a pure virtual function Procedure land () Virtual; abstract; // Landing pure virtual function function modal (): string; virtual; abstract; // look up type pure virtual function END; then, we derive two subclasses, helicopters and jets from Plane (jet): copter = class (plane) private fModal: String; public constructor Create (); destructor Destroy (); override; procedure fly (); override; procedure land (); override; function modal (): string; override ; end; jet = class (plane) private fModal: String; public constructor Create (); destructor Destroy (); override; procedure fly (); override; procedure land (); override; function modal (): string; override; END; now we have to complete a aircraft control system, there is a global function Plane_FLY, which is responsible for taking off the plane passed to it, then, just like this: Procedure Plane_Fly (const pplane: plan); begin pplane.fly (); end; you can make all the aircraft passing it (Plane's sub-object) normal take off! Whether it is a helicopter or a jet, even now does not exist, will increase the flying saucer in the future. Because each subclass has defined their own takeoff ways. It can be seen that the Plane_Fly function accepts the parameters of the Plane class object reference, and the actual passed to it is a Plane's sub-class object, and now I recalled the "polymorphism" described in the beginning: Multico is allowed to set the parent object setting Become a technology equal to one or more of his child objects, after assigning the value, the parent object can operate in different ways according to the characteristics of the child objects currently assigned to it. Obviously, Parent = Child; is the essence of polymorphism! Because the helicopter "is a" aircraft, the jet is also "a" aircraft, so all the operations of the aircraft can operate, at this time, the aircraft class is an interface.

The nature of the polymorphism is to assign a pointer to the child class to the parent type pointer (referenced in OP), as long as the assignment occurs, the polygon is generated because "up-map" is implemented. The most common examples of the application polymorphism are very common. In the VCL library of Delphi, the most typical is: TOBJECT class has a virtual Destroy fiction function and a non-virtual free function. The free function is called Destroy. So when we call any object (all sub-objects of TOBJECT). After (), it will execute TOBJECT.FREE (); it will call the destructor DESTROY () ;. This ensures that any type of object can be properly destructed. Polymorphism is the most important feature of object-oriented, this article is not a diloy, and there are still a lot of content. If possible, I hope that there will be subsequent discussion.

转载请注明原文地址:https://www.9cbs.com/read-2060.html

New Post(0)