Object-oriented thinking

Why object to be oriented?

In order to solve the object-oriented system maintainability, scalability, reusability. Time process-oriented programming, the function are written dead, then there is a case, we need to modify some part of the back of the system function, may move to a place, you need to re-write the entire code again. "Westward Design Patterns" This book is a good explanation of this problem: Cao Cao attack Soochow Three Kingdoms period, Cao Cao poetry: "! Drinking songs, Zhenshuang life", when the stereotype out of Cao Cao feel bad, craftsmen changed "wine and song, life is really cool," rigid and out, Cao Cao and feel bad, craftsmen changed to "wine and song, life geometry." Here embodies the object-oriented benefits, if at that time have movable type, then you only need to change the corresponding word can be changed, rather than re-engraved all over again, which is called maintainable. Dog run, the cat will run, so long as they can run, I can not run this action as a benchmark? So that all can run things, I have come to realize through this benchmark, then that is expandable, which is the interface. Of course, it can not also be extended through the interface inheritance. The dog will run, trained dogs also ran jump, it is not possible through inheritance, so that new dog (dog training) has a jump function, then it would be scalable, but also reuse.

Of course, the process-oriented programming can be realized by means of reusable functions. But next time if I can make a chicken running and jumping, how to do? Copy the code dog training? No! When duplicate code more to a certain extent, maintenance time, and that is a disaster. For example, the calculator will calculate and display problems, if you write separately, then behind me to maintain when you can modify to change where I want to do to modify, post you want to join the new calculation method, then directly to the computer's maintenance modified class gone on the line.

Why in class?

Class is a template, I want to create a bright student and a petit Xiaogang students, then we can directly through the template, filling the corresponding data can be. If not on the class, then create two students, will be repeated write a lot of code. There is, like human beings, the head, hands, feet and other organs, they have different functions. Like the main method as head, hands and feet by calling the head of each of their respective complete the things on it. A class to do a thing.

Why use polymorphism? It is possible to eliminate the coupling between the type having an alternative multi-state already exists for the code, which is the principle of online upgrade. Polymorphism is scalable code. Add a new sub-class does not affect the existing class of polymorphism, inheritance, and other operational and operational characteristics.

Why do we engage in the code so complicated?

In fact, object-oriented code is simplified, process-oriented programming everything blends together the program is complex, difficult to read changes. Object-oriented, the different division of labor, modify the code easier to read.

Because the amount of code is really too small, not very accurate representation.

 

Old nine school community members produced

Author: Liu Chengmeng

Guess you like

Origin www.cnblogs.com/ljxt/p/11579775.html