Longsheng Nine ---- Polymorphism

zhaozj2021-02-17  53

Longsheng nine

---- Polymorphism

Author: HolyFire

One Dragon Sheng Jiuzi, the child is different. This truth is that there are no two or more things in the world are exactly the same, and there is a similar side between things. Of course, there are different different sides. Only express things can be completely expressed in consideration of different and the same consideration.

The properties of things have different sides. We know that a way to understand the things is to observe, use the reaction to different light to identify objects, so we give an attribute and color of things. Sometimes some of the color of some objects is fixed, white clouds, blue sky, green grass, can't help but be too dedicated.

However, some aspects are uncertain, there is a saying: "The moon is cloudy and sunny, people have joys and sorrows", this sentence is not enough to explain this truth. In our Chinese, sometimes some parts will be omitted. "Do you have it today?" What is it, "breakfast", "afternoon tea", "bread", do not know. Here will eat abstraction, this sentence can be said in the morning, at noon, in the evening said, any suitable period of time can be said. Interested place, we don't know what to eat, when you eat, but you can use it, briefly express your greetings. This simple setting of the film is widely used in life in a specific usage of the specific usage. "I cut!" Expressed a person who had to do, what happened to something, only interested talents will pay attention. "I am sick." Expressed a state of a person, what is the disease, not everyone wants to know.

Polymorphism - is a different aspect of the refers

Polymorphism is a complicated application, it is difficult to fully explain it, so understanding its thoughts will become a focus. In different cases, adhering to its idea, achieving polymorphism in different ways.

Remember the virtual function in C , I have said in "post-in ---- virtual functions", the virtual function can be dynamically changed, and this feature can be used to achieve our goal. Different languages ​​use different methods, the mechanism of implementation is the interface provided by a type that can be changed. This will reflect this idea.

In order to better understand, we implements an example with C .

We know that it is necessary to work, work, work is a human social behavior, almost everyone is working. There is a variety of people in the world, what is the distinction between people, that is, their career, people of different occupations do different things.

Students' work is learning, the driver's work is driving, the work of hawkers is selling goods, the programmer's work is to write code.

Now we have analyzed that there are many people in the world, people must work.

人 {工作 = Uncertain}

Student's work is learning

Student {People, Work = Learning}

The driver's work is driving

Driver {人, work = driving}

Handwalk work is to sell

Hand dealers {people, work = trafficking}

Programmer's work is programming

Programmer {People, Work = Programming}

#include

Unsing namespace std;

Class Man {

PUBLIC:

Virtual Void Work (void) = 0; // Pure virtual function, there is no job in his work.

}

Class Student: Public Man {

PUBLIC:

Void Work (void) {cout << "I'm Learning." << endl;};

Class Chauffeur: Public Man {

PUBLIC:

Void Work (void) {cout << "I'm driving." << endl;};

}

Class Vendor: Public Man {

PUBLIC:

Void Work (void) {cout << "I'm vending." << endl;};

}

Class Programer: Public Man {

PUBLIC:

Void Work (void) {cout << "i'm code;" << endl;};

}

void main ()

{

Man * Which [5];

Which [0] = new student;

Which [1] = new chauffeur;

Which [2] = new vendor;

Which [3] = New Program;

Which [4] = NULL;

For (INT i = 0; Which [I]; i )

{

Which [I] -> Work ();

Delete Which [I];

}

}

operation result

I'm learning. // virtual function table pointer points to the virtual function table

I'm driving. // virtualistic table pointer pointing to Chauffeur's virtual function table

I'm vending. // virtual function table pointer pointing to Vendor's virtual function table

I'm codeing. // virtual function table pointer pointing to the programer virtual function table

We saw that the category increased virtual function table and virtual function table pointer due to the use of virtual functions.

Which [0] = new student;

We analyze this pointer, he point to a student object, and this pointer is a man object, when the STUDENT object is created, the virtual function table pointer points to the STUDENT virtual function table, so which [0] -> Work () The call is the STUDENT :: Work () in the virtual function table in Student :: other push, not difficult to run the result.

The class of different occupations has achieved different functions with the same interface. Our purpose is reached, the code is very simple, it is obvious, here C has once again realized the object-oriented thought.

2001/8/17

Ding Ning

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

New Post(0)