[Object-oriented discussion] interface and Polymorphism

Source: Internet
Author: User

[Object-oriented discussion] interface and Polymorphism

People are always very busy, but a person is a person and there is no separation technique.

Assume that Wang Dazhu is the principal of Guangming Middle School, a member of the village committee of Guangming village, and his son's father.

We can think like this: Wang Dazhu is a specific implementation object of a class. This class is called "Wang Dazhu class", and Wang Dazhu class implements three interfaces: "I Guangming Middle School Principal", "I Guangming village committee member", and "I father ".

The image is shown as follows:

 

We can call these three interfaces as the three "identities" of the class ".

We can create a king pillar like this:

= New ();

In this case, the variable "" represents an instance of the [class. This is okay, right?

 

The dramatic thing is coming.

> What is the identity of "Wang Dazhu" in school? From a normal perspective, he should be the principal's identity and be able to manage all aspects of the school.

> What is the identity of "Wang Dazhu" in the village committee? From a normal perspective, he should be a member of the village committee and participate in meetings and discussions of the village committee.

> What is the identity of "Wang Dazhu" at home? From a normal perspective, he should be the father of his son and take the child with his wife at home.

 

So the following code is carefully read:

// Wang Dazhu wants to hold a teacher-student meeting as the principal. I. President of Guangming Middle School, Mr. Wang = President of wangdazhu as I Guangming Middle School; and President Wang holds a teacher-student meeting ();

We know that an interface can define variables or instantiate objects (classes used for instantiation must implement this interface ).

Now, I use the identity of I Guangming Middle School principal to define the variable "Principal Wang", or define this work permit. With this work permit, the man "Wang Dazhu, the identity will be converted to "President Wang", but "President Wang" is still Wang Dazhu, not the second person. That is to say, I use the new keyword "" above, and there is only one object (person) in the memory (EARTH), the variable "" defined by the interface, it only points to the wdz and gives him the identity of the principal.

For example:

 

Those familiar with C ++ pointers and memory stacks must be familiar with each other. The left side is the heap, the right side is the stack, and the heap is the real data, the "Wang Dazhu" and "Wang "on the right are just some variables. The real relationship between data and variables is reference here.

However, interface programming has a different feature and is also an extremely important feature: interfaces have their own member variables.

In other words, "Wang Dazhu" is not the "principal" of the school at home. The function of the principal to notify the teacher and director is gone! Do you want to send a command to your wife and son? Search.

This is an identity issue. interfaces can provide different identities for objects on the stack to implement different functions.

 

Let's take a look at the following code:

// Wang Dazhu is going to the meeting. I am a member of the Guangming village committee, Wang, as a member of the Guangming village committee, and Wang. go to the village for a meeting (); // Wang Dazhu will go home and cook for his son. I Father Wang dad = I Father Wang, as a member; I Father Wang. cook for your son ();

Can readers imagine it themselves?

 

This is the interface polymorphism. In fact, there is a more complex inheritance relationship between interfaces and abstract classes in real programming. Please write it later.

Appendix: three features of OOP

Encapsulation, inheritance, and Polymorphism

These three features continue to the "Design Pattern" of programming and later higher-level programming.

Related Article

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.