"Learning Python with You Hand in Hand" 39-Object Oriented

In the last article "Learning Python with You Hand in Hand" 38-the second stage summary , we have summarized what we have learned before. Next, we will use several articles to introduce you to a very Important programming methods, or programming ideas.

Why is it a programming idea, because it does not involve new grammar, commands, functions, etc., but based on the knowledge we have learned before, by changing the structure, running logic, execution order, and even changing the program The core theme is to realize a new way of thinking and programming logic, which is object-oriented programming.

The above paragraph may not be well understood, or even accurate, because it is just my description of this new programming idea.

To be honest, the content of this part may be much more difficult to understand than what we have learned before, including myself, I am also preparing to start learning this topic several times. I don’t understand it, let it go, and then pick it up again after a while. Still unable to understand, put it down again. It's been over and over again for more than half a year. Only recently did I finally feel a sense of epiphany and understand the meaning of it, and dare to continue to update the series of articles "Learning Python with You Hand in Hand". This is also an important reason for the suspension of more than half a year in addition to the epidemic and work reasons.

Because if you don't fully understand this point, I really dare not continue to learn Python with everyone against my will. Although this piece can also be separated, it is not so perfect in terms of the logic and completeness of the whole learning.

Therefore, I will try my best to introduce object-oriented programming methods and programming ideas in the next few articles in the most popular (maybe more verbose) way. I hope everyone can understand them as soon as possible. And master.

Of course, as I just said, I just learned about object-oriented. If there are any inaccuracies in the introduction, I hope you can forgive me and make valuable suggestions. I will follow along. In the article, make corrections in time, and share my new learning experience with you.

Because this piece of content is indeed a bit difficult, if you have anything you can't understand during the learning process, please feel free to leave a message and discuss it together, hoping to help you overcome this difficulty as soon as possible.

You can also put it aside as I did before, and continue to learn the following content, it will not have any impact. After a period of time, look back and see. I believe there are a few repetitions at most, and everyone will definitely understand it.

The above is a little bit of my experience and suggestions. Before I start learning object-oriented, I would like to share it with you. I hope it will be helpful to everyone in the future.

Next, we will officially start learning object-oriented programming, everyone!

1. Two methods of programming:

Just now, object-oriented is a new programming method or programming idea, so what methods and ideas did we use in programming?

That is process-oriented, including the examples we listed in the process of learning the basics before, and the two relatively large (relative to other examples) programs of "rock, paper, scissors" and "guess the number", which are all based on The way of the process is writing the program.

So what is object-oriented, what is process-oriented, and what is the difference between the two?

2. Process-oriented

First of all, literally, process-oriented is easier to understand, that is, process-centric. In order to achieve any goal, what needs to be done first, then what can be done. More popularly, to get to point D, start from A, go through B, then to C, and finally to D.

If we take our previous "rock, paper, scissors" as an example, we need to know who wins. We need to set the rules of the game first, then let the user punch, then let the computer punch, and finally judge the winner according to the rules.

The starting point of the entire program design is consistent with the order in which we consider one thing. This is a process-centric programming idea, that is, process-oriented.

3. Object-oriented

From this we can see that object-oriented is object-centric.

This object is everything in our world. It can be a water glass on our desktop, a fish in the sea, a beautiful shot, or an unclear love.

In short, everything that can be said, cannot be said, concrete, and abstract can ultimately be called "objects."

Therefore, object-oriented programming is a programming method with these "objects" as the core.

4. Difference

When we adopt the object-oriented programming method, the question to be considered is no longer "how to do it first", but becomes "what (that is, the object) is what (attribute) can do (method)".

The explanation in brackets is to distinguish the meaning of several "whats", which will be discussed in detail later. You can ignore it here. The key is to understand the phrase "what is and what can do".

Compare it with "what to do first, what to do, and what to do last". Can you feel any difference?

The former is introducing this (object), while the latter is concerned with how to do it (process).

This is the difference between object-oriented and process-oriented.

Example 1. Rock Paper Scissors

If you don’t understand, let’s take “rock, paper, scissors” as an example to see what objects the program has and how it should be designed from an object-oriented perspective.

First of all, the most important object is the contestant (object). The user is the contestant (instantiated), and the computer is the contestant (instantiated). What can the contestants do? Able to "punch" (method).

The parentheses here also don't need to pay attention to them. After I talk about these concepts later, I can understand their meaning when I come back and look at them (the same below).

Another object is a rule (object). What is a rule? It is "Rock wins scissors, scissors wins cloth, and cloth wins rock" (attribute). What can rules do? "Judge Win or Lose" (method).

Okay, now all the objects are in place. It is clear what the objects are and what they can do. The following is the process of organizing the program.

First, the user punches (user. punches); second, the computer punches (computer. punches); finally, the rules determine whether to win or lose (rules. determine).

Seeing this, everyone will ask, this seems to be a process, and it is no different from before?

That's right, this is indeed a process. After all, we are a process more than this "rock-paper-scissors". It is impossible to change the order of judging wins and losses after punching.

But have you noticed that this process is object-centric, which is the biggest difference from a process-oriented process.

You can go back and see how this game is described in the process-oriented process. It is to "set" the rules of the game, then "make" the user punch, then "make" the computer punch, and finally "judge" win or lose according to the rules.

Each process is centered on the process in quotes.

The object-oriented description method is that the “user” punches, then the “computer” punches, and finally, the “rules” judge the winner.

Each process is initiated by an object (user, computer, rule), describing what the object does (punching, judging), and finally connecting the things done by all objects in order to form a complete program .

Speaking of this, I don’t know if you have understood a bit. Let's use another example to continue to experience the difference between object-oriented and process-oriented.

Example 2. Wash clothes

If we have clothes to wash in a washing machine, what should this process be like?

The first thing we think of should be the process-oriented approach:

1. Open the washing machine door

2. Put the clothes in

3. Close the washing machine door

4. Turn on the power

5. Wash clothes

6, spin dry clothes

7. Dry clothes

Just like the process in "Rock, Paper, Scissors, and Paper", each action is combined in sequence. It doesn't matter who opens the door and who washes the clothes.

If it is object-oriented, the first thing we have to think of is not what we do first, but what (object) can do (method).

The objects here are obviously people and washing machines. People can open the door, put clothes, close the door, and turn on the power, respectively, while the washing machine can wash, spin and dry.

After that is the washing process, and this process is based on the object as the core, or to be executed with the object as the starting point:

1. People open the washing machine door (people. Open the washing machine door)

2. People put clothes in (people put clothes in)

3. People close the door of the washing machine (people. Close the door of the washing machine)

4. People start the power supply (people start the power supply)

5. Washing clothes by washing machine (washing machine. Wash clothes)

6. Washing machine to spin dry clothes (washing machine. Dry clothes)

7. Washing machine drying clothes (washing machine. Drying clothes)

Although it is still the same process, each action is performed by the object, and the "who" of the "who" is the most important.

The above is a literal understanding and two examples to introduce you to process-oriented, object-oriented and the difference between them.

Just like me, the first time you encounter these concepts will be very confusing, especially if you haven't seen too many such examples, so it is difficult to appreciate the difference between them.

The good thing is that, based on my understanding, I have explained the similarities and differences between the two concepts in a popular way, and used examples as a comparative illustration. It may be easier to understand.

But if the first contact may still be difficult to understand, but it’s okay, as we said at the beginning, you can let it go and watch it again in two days, or continue to learn the following content, and then come back to learn, there may be problems The same recognition.

Later we will continue to explain the object-oriented programming method, but it is not explained in popular concepts like this article, but to enter the specific structure and writing rules of object-oriented programming, and at the same time can understand The meaning of a lot of words in brackets in today's article has been changed, so stay tuned.

 

image

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

To read other articles in the "Learning Python with You Hand in Hand" series, please follow the official account and click on the menu selection, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

"Learning Python with You Hand in Hand" 6-String Identification

"Learning Python with You Hand in Hand" 7-Index of Strings

"Learning Python with You Hand in Hand" 8-String Slicing

"Learning Python with You Hand in Hand" 9-String Operations

"Learning Python with You Hand in Hand" 10-String Functions

"Learning Python with You Hand in Hand" 11-Formatted Output of Strings

"Learning Python with You Hand in Hand" 12-Numbers

"Learning Python with You Hand in Hand" 13-Operation

"Learning Python with You Hand in Hand" 14-Interactive Input

"Learning Python with You Hand in Hand" 15-judgment statement if

"Learning Python with You Hand in Hand" 16-loop statement while

"Learning Python with You Hand in Hand" 17-the end of the loop

"Learning Python with You Hand in Hand" 18-loop statement for

"Learning Python with You Hand in Hand" 19-Summary of the first stage

"Learning Python with You Hand in Hand" 20-List

"Learning Python with You Hand in Hand" 21-Tuples

"Learning Python with You Hand in Hand" 22-Dictionary

"Learning Python with You Hand in Hand" 23-Built-in Sequence Function

"Learning Python with You Hand in Hand" 24-Collection

"Learning Python with You Hand in Hand" 25-List Comprehension

"Learning Python with You Hand in Hand" 26-Custom Functions

"Learning Python with You Hand in Hand" 27-Parameters of Custom Functions

"Learning Python with You Hand in Hand" 28-the return value of a custom function

"Learning Python with You Hand in Hand" 29-Anonymous Functions

"Learning Python with You Hand in Hand" 30-Module

"Learning Python with You Hand in Hand" 31-File Opening

"Learning Python with You Hand in Hand" 32-File Reading

"Learning Python with You Hand in Hand" 33-closing the file

"Learning Python with You Hand in Hand" 34-File Writing

"Learning Python with You Hand in Hand" 35-Data Storage

"Learning Python with You Hand in Hand" 36-Error and Exception Handling

"Learn Python with You Hand in Hand" 37-Program Reconstruction

"Learning Python with You Hand in Hand" 38-Summary of the second stage

 

For Fans: Follow the "also said Python" public account, you can download the sample sentences used in this series of articles for free.

Also talk about Python-a learning and sharing area for Python lovers

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/112417855