The realization of C + + object-oriented, reflection and redemption

Source: Internet
Author: User

This blog I have been brewing for a long time, but has not written, the main reason is to feel that they are still difficult to master such a grand proposition, for me such a novice is not even the entry level of people, talk about this topic will inevitably have a paper-like, so laughable. But the main purpose of my technical blog is to record, Share your own technological growth on the road of some personal understanding, as long as there is their own thinking, even if a few years later looked back to look naïve and even the wrong place, it is no harm.

In fact, in the long run, I always talk about programming my heart will be guilty, popular point is a little bit empty, this is not to say that I can not write the program, but feel that they do not have a framework for the design of a thinking, as if the root backbone. Today you let me write a PageRank algorithm, I could give you out; Tomorrow you let me achieve three-dimensional human animation, I can work out a little bit. But in the face of some basic problems, I have absolutely no clue, for example:

Why exactly do you need classes? The book is clearly said to use the class to express the concept is the essence, but you see what I do not need not to pagerank,hits algorithm to achieve good ...

What is object-oriented? Open up a textbook in a book. There are three main characteristics of object-oriented: encapsulation inheritance polymorphism ; the other is that object-oriented is the simulation of the real world, everything is the object, the object through the message mechanism to communicate ; And then a few days to see a video inside the teacher is the ultimate goal of object-oriented is to reduce the duplication of code AH ... And then it's all messy.

and design patterns? Only yesterday to hear people say design patterns and refactoring is the software development of the day of the Dragon Slayer, today an open and then find out how to answer the following exclusively are all in the spray design mode? Open Gang of Four's book to see I have a total of 23 modes, this is a bad memory for me this kind of people can be good, Do I have to memorized the 23 design patterns back to the heart, and then use the time to walk through the mind to see which fit? can be 23 or too many to feel that they do not remember ah ...

Do you say these questions have meaning, feel are some metaphysical East Ah, with practical programming practice feel not border, as if have not seen around people have similar confusion?

My answer is very clear, very important, and is the first important, at least for me personally. As if the night in the boundless sea of despair floating, suddenly see a distant lighthouse feeling.

The thought framework that I said before is actually a more professional noun called the programming paradigm .

The so-called programming paradigm is the abstraction of specific problems when you are programming, while object-oriented is one of the programming paradigms.

What do we really understand about object-oriented, and what is the soul of it?

To save me from the chaos of the appearance is to know the last predecessor invalid's answer, http://www.zhihu.com/question/20275578/answer/26577791 this answer I looked before and after 10 times, even wrote on the paper several times, At that time there was a feeling of long drought, because it was a real answer to some of the long-standing confusion in my heart. Now look back, this answer has its own limitations, I will talk about later, but it helped me to establish a starting point for thinking.

Since then I have made clear in my mind the following points:

Object-oriented ≠ encapsulation + inheritance + polymorphism, rather than encapsulation + (inheritance/polymorphism); Inheritance (interface inheritance) and polymorphism are the positive and negative sides of a coin, rather than equal relations, polymorphism is the purpose, inheritance is the means of realizing polymorphism.

The object-oriented benefits are:

1. Through the encapsulation of hidden internal implementation, exposed to the user interface, so that the interface and implementation of separation, users use the class to be more secure;

2. polymorphism is normalized . The normalization is to simplify the use of the user. By inheriting a common base class, the base class provides an interface, the derived class is responsible for implementation, in C + + by pointing the base class reference or pointer to the derived class object, you can get the use of concise, Consistent external interfaces enable the benefits of a colourful and cluttered inner execution logic .

If the 2nd makes you think abstract is not intuitive, I will give a specific example of life you can immediately understand the benefits of polymorphism. For example plug plug, electrical plug of the number of appliances, the wiring board behind the Iude line is more complex, but these do not matter, to the user, Just plug in the hole and stick it in. What is normalization, this is normalization, word, simplifying user use logic. Of course, the user does not have to pay attention to the circuit behind the wall, or the internal circuit mechanism of the tap, only such a plug (using the interface), it is OK.

The object-oriented benefits are these two points.

To be Continued ...

The realization of C + + object-oriented, reflection and redemption

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.