This series of articles will be taught in an easy-to-understand dialogue mode. The dialogue will cover the general problems of novices in learning. This series will continue to be updated, including other languages and actual combat will be taught in the way of dialogue, basic programming language teaching is suitable for zero-based beginners, and the actual combat courses will also be gradually updated.
If you have something you want to learn, you can leave a message in the comment area and continue to update it according to everyone's requirements. Even if you like more than 100,000, I will update the next article even if it turns to dust. (Definitely not, it will be more.)
If you have any questions, you can leave a message in the comment area, or chat privately with me and my friends to communicate and make progress together~
If you like the content, please follow me, comment, like, and bookmark. This is very important to me, thank you~
My other tutorials
Click to enter the tutorial and there will be a catalog
table of Contents
"You can't learn Python to game combat even if you look at the chat history? It’s too much. "It’s so easy to write games in python. I can use the keyboard to control the character at will. [Python game combat 04] —— The keyboard controls the movement of the character
"You can't learn Python to game combat even if you look at the chat history? Too much food" (14) In fact, you can get started with python object-oriented in 3 minutes -create classes and use class methods
"You can't learn Python to game combat even if you look at the chat history? Too much food" (8) Let’s start making a digital game -digital games start to guide the package
About the Author
Author name: 1_bit
Introduction: CSDN blog expert, TOP5 blog star in 2020, signed author of Blue Bridge. In 15-16, he was broadcast live on the Internet, leading a group of programmers to embark on the road of programmers. Welcome everyone to join me and consult me for relevant information. If you are confused, you will find the answer.
Blog address: https://i1bit.blog.csdn.net
🐰Little C: In this section we will learn object-oriented content.
👸Xiaoyuan: I don’t have an object, how can I program with my object? Did you do it on purpose?
🐰Little C: Object-oriented is a concept of programming, what are you thinking about?
👸Xiaoyuan: You mean that the code has objects but I don't? Deliberately.
🐰Little C: I...so do you learn?
👸Xiaoyuan: Learn to learn, hurry up, I want to eat code dog food.
🐰Little C: Object-oriented is actually very simple, you can understand it as a category. For example, if you play a game, there are many npcs in it. In fact, these npc characters have similar attributes, including hair, eyes, nose, height, and gender. To create each one, we only need to create a specific type, and use this type each time.
👸Xiaoyuan: I understand a little bit, but I don't fully understand it, so let's use an example to explain it.
🐰Little C: To create a class, you need to use a keyword class, followed by a name, and finally use a colon to indicate the end.
👸Xiaoyuan: Then what?
🐰Little C: Then we can create some variables in this type, and these variables are the attributes of this type. The class we created here is called Human, which is used to describe people. People have attributes such as name, age, gender, and height. We can create in this class:
class Human: name="未命名" age=18 height=170 sex="男"
👸Xiaoyuan: Have you created 3 attributes in this way?
🐰Little C: Yes, in this case, as long as we use this type to create specific objects, such as Xiaoming and Xiaohong, we can directly create objects of this category without too much code, and we can directly have these attributes.
👸Xiaoyuan: How to do it?
🐰Little C: It's very simple. Look at the code below to create a specific type.
👸Xiaoyuan: Use a parenthesis in the type name directly? Same as function?
🐰Little C: Yes, this is creation.
👸Xiaoyuan: After creating it, directly give xiaoming a specific type?
🐰Little C: Yes, but this does not represent classes and objects very well. We can create a class method, that is, what functions does this class have? Look at the code in the following class.
class Human: name="未命名" age=18 height=170 sex="男" def say(self): print("hello")
👸Xiaoyuan: Huh? I think there is a function in the class. This function has a parameter of self. What is this?
🐰Little C: self means to point to the current object itself. All functions in the class need to use this self, but this self does not need to pass parameters, because self means that the current function belongs to this class.
👸 Xiaoyuan: I don’t understand.
🐰Little C: Let's add a new parameter.
class Human: name="未命名" age=18 height=170 sex="男" def say(self,say_str): print(say_str)
👸Xiaoyuan: In this case, you can pass a parameter, which is the content displayed by print in the function.
🐰Little C: Yes, let me show you how to use this function.
class Human: name="未命名" age=18 height=170 sex="男" def say(self,say_str): print(say_str) xiaoming=Human() xiaoming.say("Hello")
👸Xiaoyuan: Can you use this function directly with a decimal point? And there isn’t a self in front, so why don’t you pass parameters?
🐰Little C: Because I said just now, self means that this function points to the current class, and you don’t need to pass parameters, just treat it as if it doesn’t exist. Then you can understand the decimal point as the meaning of "of", that is to say, use the say method of the created object xiaoming, so that it can be used.
👸Xiaoyuan: Hahaha, it's solved, I have the result directly.
🐰Little C: Hahaha, okay? Then we create a few more methods.
class Human: name="未命名" age=18 height=170 sex="男" def say(self,say_str): print(say_str) def myname(self): print(self.name) def myage(self): print(self.age) def myheight(self): print(self.height) def mysex(self): print(self.sex) xiaoming=Human() xiaoming.myname() xiaoming.myage() xiaoming.myheight() xiaoming.mysex()
👸Xiaoyuan: What does the code in these methods such as self.sex mean?
🐰Little C: It means that self refers to the current object itself, and self.sex is to get the sex variable of the current object itself. At this time, just display it directly.
👸Xiaoyuan: Hahaha, solved.
🐰Little C: Let’s explain the classes in detail in the next section.
👸Xiaoyuan: Hahaha, Hao Le.