To understand object-oriented, just read this article

Introduction

Regarding the understanding of object-oriented, most people still understand the concept, but in the actual implementation process, they still maintain the process-oriented idea when developing and writing code. Below, I will reorganize what object-oriented is through this blog.

1. Characteristics and foundation of object-oriented

feature

Encapsulation, inheritance, polymorphism

Base

abstract

2. Why is it said that the basis of object-oriented is abstraction?

If you want to truly understand this sentence, first take a look at the following question:
How to understand the following sentence? How does this relate to object-oriented abstraction?
1) We should focus on how many people do things, rather than how many things are done.
2) We should pay attention to who will do things, rather than how to do things.

Analysis:
Let’s first analyze the literal meaning of these two sentences. During the analysis process, we need to change the focus of this sentence, from focusing on things to focusing on people (with the subject as the main subject). For example, "how many things to do" means stating how many things there are, or focusing on things, focusing on event-related content. From the perspective of software design, it is still process-oriented, and "focusing on how many people will do things" is Thinking about this matter from a human perspective, we switched the focus of this matter to people. From a software design perspective, we switched from analyzing the problem from a process-oriented perspective to an object-oriented perspective. to analyze the problem. The second sentence is also the same as above.

Example:
But analyzing these two sentences alone may not be easy to understand. Let me give you an example: "I'll get you a glass of water." Let's analyze this sentence from a process-oriented perspective and an object-oriented perspective.

Process-oriented : The two subjects "I" and "you" are involved in the matter of "going to fetch water".
We are object-oriented in the analysis around "getting a glass of water" : we first decompose this sentence into as many objects as possible. In this sentence, "I", "you", "water", "water bottle" Container", "Place to draw water from". These are the objects we decompose through this sentence. We can classify and encapsulate these objects. For example, the "I" and "you" in this sentence can actually be abstracted into "participants". We are doing this After analysis, the understanding of this sentence has been converted into object-oriented thinking with objects as the main body. At this time, we can develop code by instantiating the corresponding specific class.

3. How to analyze from an object-oriented perspective

Here I have compiled a few specific steps to help you analyze problems and business from an object-oriented perspective:

  1. The first step is to list the abstracted objects.
    For example: split the object subject in the business, split "I will get you water" into "me", "you", "water", and "water container" ”, “Place to fetch water” objects.
  2. In the second step, the abstracted objects are merged and classified and further abstracted (further abstraction can be carried out as appropriate),
    such as: participants, tools used, locations (UI, pages), materials (parameters)
  3. The third step is to implement code automation and use factory methods.

4. Summary

To understand object-oriented thinking, the most important thing is to understand abstraction. Abstraction is the most basic, important and critical in object-oriented. When we abstract the objects that need to be analyzed from the business, we should not just stop at the superficial objects, but also pay attention to the fact that they contain some transparent objects. The most important thing is that we can only abstract these transparent and virtual objects. .

Guess you like

Origin blog.csdn.net/zwb568/article/details/124591451