C # three features of object-oriented,

Source: Internet
Author: User

C # three features of object-oriented,

I,Encapsulation:
We can take everything in the world as an object, so here we take people as an example, a person must be an object.
So what is encapsulation?
Encapsulation means that this person has to do one thing. All the tools he needs are carried on his own, and all the technologies he needs are packed in his own mind. You can do this without the help of others. What are the benefits of this? The advantage is that if I want to instruct this person to do one thing, I don't need to know how he did it or help him complete it. As long as I know the result, after he completes, he will be OK. As for what he did first and what I did without interference, it will help him do this thing faster and better, and I will also save trouble.

The written explanation is as follows:
Each object contains all the information required for operations. Therefore, the object does not have to rely on other objects to complete its own operations.

II,Inheritance:
For example, each of us has some common characteristics and needs to do the same thing. For example, a person has a head, two arm, and two feet. This is a common feature. You have to eat, drink, and do the same thing. So if we want to declare many people and everyone will have these features, isn't it necessary to write a lot of repeated code? Therefore, we can first create a person's parent class. This parent class does not represent a specific person, but a virtual person with all the common characteristics of the person. The next time we want to instantiate a specific person, we only need to inherit the above "virtual person", then he will have all the common characteristics of the person. In this way, we can stop writing the repeated code.

Of course, the purpose of inheritance is not only to save code, but also to implement the subsequent polymorphism functions. Beginners only need to understand inheritance and can write much less code. The rest needs to be understood slowly in the project.

The written explanation is as follows:
Object Inheritance represents a "is-A" relationship. If two objects A and B can be described as "B is A", B can inherit from.

Note:
If A inherits B, A not only has all of B's proprietary features, but also has its own unique features. For example, in the above example, a person who inherits "virtual" will not only have a head, two arms, and two feet, but also need to eat and drink water, he may also program. Programming is his unique feature, because not everyone can program.

3,Polymorphism:
There must be inheritance before there is polymorphism. Only when multiple classes inherit the same class at the same time can there be polymorphism.

Let's take people as an example. We have mentioned that people have to "eat. "Eat" is a method in the class. We have written this method in "virtual people" because it is done by people. However, everyone has different ways to eat and use chopsticks and forks, rice or pasta. So if we want to describe the course of eating for different people, we need to put this process into a specific subclass, because everyone is different.In the same method, subclass has different implementation methods. This is a polymorphism.Polymorphism facilitates program flexibility.

Note: If the method (polymorphism) of the parent class is overwritten in the subclass, this method in the parent class will not be called.

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.