Three main features of C # object-oriented

Source: Internet
Author: User

OnePackage
We can look at anything in the world as an object, so here we take people for example, a person must be an object.
So what is encapsulation?
Encapsulation is the person who wants to accomplish one thing, all the tools he needs are on his own, and all the technology he needs is in his head. You can accomplish this without the help of others. What good is this? The advantage is that if I want to command this person to do one thing, I don't need to know how he is done, and he doesn't need to help him to finish it, as long as he knows the result, he is OK. As for what he did after what I did not intervene, which helped him to better do the job faster, I myself also easy.

The written explanations are as follows:
Each object contains all the information it needs to operate, so the object does not have to rely on other objects to do its work.

TwoInheritance
Think of people as an example, each of us will have some common characteristics, have to do some of the same things. For example: People have a head, two arms, two feet, this is a common feature. All have to eat, drink water, this is to do the same thing. So if we're going to declare a lot of individuals now and everyone has these traits, then I'm not going to write a lot of repetitive code? So we can first build a person's parent class, the parent class does not represent a specific person, but a virtual person with all the common characteristics of the person. The next time we are going to instantiate a specific person, we just have to inherit this person from the "virtual Man" above, then he has all the common traits of man. In this way, we can not write the duplicated code.

Of course, the goal of inheritance is not just to save code, but to implement the polymorphic functionality behind it. Beginners only need to understand the inheritance can write a lot less code, the rest of the need to slowly understand the project.

The written explanations are as follows:
The inheritance of an object represents a "is-a" relationship, and if two objects A and B can be described as "B is a", then B can inherit from a.

Attention:
If a inherits B, then a does not only have all of the characteristics of B except the private property, a can also have its own unique characteristics. For example, the above example, a successor to the "virtual person", then he has a head, two arms, two feet, to eat water, he may also be programmed. Programming is his unique feature, because not everyone will be programmed.

Three polymorphic
There must be inheritance before there are many states, only multiple classes inherit the same class at the same time, there is a polymorphic such argument.

Or a man-made example, before we have said that people have to do "eat" such a thing. "Eat" this thing, reflected in the class is the method. Because it is people are to do, so we in the "virtual person" in writing this method, but everyone has a different way to eat, useful chopsticks also useful fork, have to eat rice also have to eat pasta. So if we're going to describe the process of eating different people, we're going to put this process into a specific subclass, because everyone is not the same.In the same way, subclasses have different implementations, and this is polymorphism., polymorphism contributes to the flexibility of the program.

Note: If you override the method (Polymorphic) of the parent class in the subclass, the method in the parent class will no longer be called.

Three main features of C # object-oriented

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.