In fact, python object-oriented can get started in 3 minutes(14)

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

"Can you learn C language when you read the chat history? Too bad (1) I ask basic questions in the big group and nobody cares? ——Study Suggestion

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? It’s so cool" the game characters are starting to move, so handsome! [Python game combat 03] —— character movement

"You can't learn Python to game combat even if you look at the chat history? That’s too cool" This is the first time I have used code to create a window [python game combat 02] -create game scenes

"You can't learn Python to game combat even if you look at the chat history? That’s too cool" This is the first time I have used code to create a window [python game combat 01] —— Create a window

"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" (13) python dictionary 2 minutes quick solution -dictionary

"You can't learn Python to game combat even if you look at the chat history? It’s too cool" (12) The list of python I never thought would be so easy to use -list

"You can't learn Python to game combat even if you look at the chat history? Too much food" (11) How many knowledge points are there in while in python—— break continue

"You can't learn Python to game combat even if you look at the chat history? Too dishes it "(10) bottomless pit cycle - early glimpse cycle

"You can't learn Python to game combat even if you look at the chat history? Too much food" (9) set of immortal python -guide package

"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

"You can't learn Python to game combat even if you look at the chat history? Too bad" (7) I wrote a Trojan horse with a function -custom function

"You can't learn Python to game combat even if you look at the chat history? Too much food" (6) Not this knowledge point, the more difficult it is to write the code -and or logical operation

"You can't learn Python to game combat even if you look at the chat history? Too much food" (5) Use the sword of the previous dynasty to cut the current official- if...else

"You can't learn Python to game combat even if you look at the chat history? Too much food" (4) Xiaobai does not know that this knowledge point will be full of errors and omissions- if and level

"You can't learn Python to game combat even if you look at the chat history? It’s too delicious" (3) says that 123 is a character, not a number -input and type

"You can't learn Python to game combat even if you look at the chat history? Too bad" (2) The plus sign in python is so fickle -variables and operations

"You can't learn Python to game combat even if you look at the chat history? Too much food" (1) Load Python artifact! Bright sword! ——Helloworld


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.

class Human:

👸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.

xiaoming=Human()

👸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.