Shishuo Xinyu: What Is Object-Oriented (inheritance II)

Source: Internet
Author: User

The first feature of object-oriented is encapsulation. Brother learned about encapsulation, encapsulation, and purpose through an example of a Disgusting garbage bag. In summary, encapsulation is to package a bunch of similar things together. Because things are similar, "classes" are used to indicate encapsulated things. This time, I want to explain the inheritance of the three main features of object-oriented.

What is inheritance? If we talk about inheritance in real life, I believe that everyone knows that inheritance can make an empty bachelor suddenly gain the n-plus things that he "Didi" left for him, such as money, such as rights, for example .... and so on. Brother likes to give an example to express his point of view. I'm sorry for the rich generation. You are going to be an example of Brother's pen. It is said that the distant and evil capitalist countries are rich in the idea of human rights and equality for all, it is also said that the dareyin temple in Hezhou, the West ox, 108,000 miles from Dongsheng Shenzhou, is rich in Buddhism with equal beings. In fact, there is no difference between the rich generation and ordinary people. They carry their heads on their shoulders. The so-called equality of all beings is like this. However, they have accumulated huge wealth and network resources for many years. Those rich second generations, through inheritance, all of a sudden acquisition of his father, his father, his father's his father ....... By the way, the Lord, you have not finished it yet. When can you say it. Dear friends
I am always so worried. I want to go straight to the theme. Maybe you would like to continue to inherit from me. Where are you talking about? Right, speaking of monkey, his father, his father, and his father... you are too thundered, let alone Darwin's evolution. Ha, what brother really wants to talk about is evolution. Don't blame him for anything. Who makes his thinking so divergent. I can't talk about it any more. If I talk about it any more, I guess I want to throw my shoes. I am not bush, and I have never practiced anything special about shoe hiding. In fact, evolution is a typical example of inheritance, from the original single-cell creature, what is it,
"Shoes and worms", after N re-inheritance, finally became the rich generation, the official generation of the ancestor. After talking about inheritance for a long time, What Is Inheritance? inheritance is a method that allows you to get a lot of things at once. The same is true for inheritance in object-oriented systems. You don't need to start from 0 to get more than N existing functions at once.

as inherit from B, tell us how to inherit from B? Don't worry, let your brother come slowly. As the saying goes, there is no white lunch in the world, and inheritance is the same. Do you want to inherit? Then you have to think about whether you want to be a son, or the son of the son, or. That is to say, to get inheritance, you have to learn to recognize dad, Ray. You are a real person. You just want to help everyone understand the object orientation and how to get involved with "recognize Dad. Well, this is not what my brother is fond. For any Program Design Language, the first step of inheritance is to recognize dad, dad is willing to leave his hard work to his son. Even if the programming language is more powerful than the inheritance in reality, for example, C ++ has three inheritance Methods: public, protected, and private. Public is the baby in the main room. You can take the dad's signboard and mix it out (use the dad's interface directly ).
protected is a little worse. It is estimated that it is a side-room doll. I never dared to mix it out with a signboard. I had to secretly use my dad's function, but it was okay, you can also use Dad's function as a messenger to pass on to your son and sun. Private is miserable. It is estimated that it must be a illegitimate child. Dad's function can only be used secretly by himself, but he cannot leave it to his son, sun, and sun. In fact, he really sympathizes with you, but there is no way. Who makes you a illegitimate child? The Ancient Emperors of the people have the saying of standing up and standing up.

Inherit from coding, doesn't it mean we should use inheritance as much as possible? Well, as the saying goes, everything in the world has its two sides. Even science and technology are a double-edged sword. What's more, what's the inheritance? We can't just know the advantages of inheritance. Inheritance also has many disadvantages, such as the one who shouted "My dad is Li Gang, I accidentally inherited some bad habits from my dad and finally harmed myself. Because inheritance cannot be controlled by you. Once you choose to find a "class" Dad to inherit from, the child will not be able to inherit from other proprietary things (such as Li Gang's Xiaomi ), you inherit all the advantages and disadvantages of the father. However, in the object-oriented field, you can freely choose your father. Unlike in real life, you are generally not allowed to recognize your father at will. It depends on your understanding of "Dad (class)". It is also a good saying. The hero of Lao Tzu and the tortoise of Lao Tzu are eight eggs, we recommend that you choose not to inherit from those heroes.

The original inheritance is not so amazing. I am a programming cainiao. I don't know when to use inheritance or when to do not need inheritance. What should I do? Generally, coder will tell you that when it is a "is a" relationship, you should adopt inheritance. It is estimated that the cainiao will ask what is "is. Who makes cainiao always have countless problems. Let's tell you a tip. inheritance is not suitable for cainiao. What really works for cainiao is a combination. In fact, the same is true for programming old birds. We need to use inheritance as little as possible, so we don't need it if we don't need it. We need to use combinations more.

I am Prince coding. I am very good at using inheritance and can also use it correctly. The only thing I don't understand is how many levels of inheritance can be inherited, but in terms of language, it seems that there is no limit to inheritance. Well, let me tell you, as long as you are not designing any framework class stuff, let alone three, that is, you can inherit three times at most. Remember. Why ??? No, you have heard the saying "rich but 3". Unless you are a good guy, otherwise, there will be more inheritance. It's easy to get involved. What will happen if you get involved? Do you know?

The landlord, inheritance is so difficult to use, Why can he become one of the three major features you have mentioned. Younger brother, you are very good at this sentence. Let your brother say it slowly. One of the biggest features of object orientation is encapsulation. However, if you only encapsulate objects, you can only play with your own classes and get a lot of unique classes. Inheritance benefitsCodeReuse, inheritance, and polymorphism will make the whole world better and very good.

What is polymorphism ??, So anxious to ask questions. Polymorphism, brother will talk about it in the next section. He has been coding for more than an hour. It's all early in the morning, so he is going to bed. In the old saying, I want to know what to do later and listen to the next decomposition.

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.