Official Python column article 36, students stop, don't miss this article starting from 0!
Previously, I wrote other functions such as file reading and file processing, which used the os library.
Originally, I wanted to share the os library. I found that this library may be difficult for beginners, so I'll talk about it later at an appropriate time, because it involves a lot of file system operations, which is more Linux operation and maintenance.
Let's take a look at object-oriented programming first, that is, OOP (Object-oriented Programming) mentioned by many people
What is object-oriented programming?
Have we heard of process orientation? It's easier to compare it together.
Simple to understand, process oriented is to solve all files with functions, simple and rough!
Object-oriented programming appeared after process oriented programming. Without object-oriented programming, many programs are not developed as usual.
Object oriented also uses functions, but there is an additional network. This network associates one or more functions with data, and then it is called a class of things, that is, the 'class' in the program
Define a class and feel it from the specific code!
The first concept of object-oriented programming is' class', class:
#This is the definition code of a class: class hello_class(): pass
Then it is called to the production object through class_name().
Upgrade the code a little. Let's see:
class hello_class(): pass #Output class information print(hello_class) print(type(hello_class)) #Create an instance object of the class print(hello_class()) print(type(hello_class()))
Add a little:
The print function outputs the result of a class object: usually < 'class full name' object at id serial number >
Here are the results:
Here we add a new knowledge point: class instance object, usually directly, instance.
The instance is an object generated by class. The type of all hello_class objects (obtained through the type function) must be hello_class.
Observation of multiple classes and objects
After reading a class, let's look at the comparison between the two classes. The results are also consistent.
The following is the definition of the two classes and the code display of the generated object:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time: 11:58 PM, November 15, 2021 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Thunder Science Committee # @XueWeiTag: CodingDemo # @File : __init__.py.py # @Project : hello class student(object): """The school committee added:__init___ The function is the initialization function of a class. This function will be called when creating an instance of a class object. "" " def __init__(self): print("hello, I am student") class programmer(object): def __init__(self): print("hello, I am programmer") class student(object): def __init__(self): print("hello, I am student") class programmer(object): def __init__(self): print("hello, I am programmer") s1 = student print(s1) p1 = programmer print(p1) s11 = student print(s11) p11 = programmer print(p11) print("*" * 16) # create object s2 = student() print(s2) p2 = programmer() print(p2) # create object s3 = student() print(s3) p3 = programmer() print(p3)
A little explanation:
The printout results of s1 and p1 variables are of type 'class'.
The print output results of s11 and p11 variables are of type 'class', but s1 and s11,p1 and p11 are unchanged.
The print output results of s2 and p2 variables are of type 'object'.
The print output results of s3 and p3 variables are of type 'object'\
Here are the results:
The initialization function is called and the object information is printed.
At this point, everyone should know the difference between class and object
Class: describes the fixed relationship between functions and attributes
(class instance) object: a living individual based on this fixed relationship, whose id is variable.
Add the attributes of the class (data part)
The school committee defines a student class and creates two student objects.
Directly copy and run the following code:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time: 11:58 PM, November 15, 2021 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Thunder Science Committee # @XueWeiTag: CodingDemo # @File : __init__.py.py # @Project : hello class student(object): def __init__(self, name): self.name = name def get_name(self): return self.name def set_name(self, name): self.name = name def study(self): print(f"{self.name} : study hard and make progress every day!") s1= student("Xiaobai") print(s1) print(s1.get_name()) print(s1.study()) s2 = student("Student fans of the school committee:Ha ha ha") print(s2) print(s2.get_name()) print(s2.study())
We see that their IDs are always different (run a few times to see).
Then each student has a name attribute (carrying name data) and three function attributes (used to obtain name, rename and learn respectively).
Then we call the study function of each student and output their learning status.
summary
The simple design of Python language makes object-oriented programming very simple and easy to define and obtain objects.
The above code is very simple, but you can feel the presentation of classes and objects well. Practice more.
By the way, if you like Python, please pay attention to the school committee Python foundation column or Introduction to Python to master the big column
Continuous learning and continuous development, I'm Lei Xuewei!
Programming is very interesting. The key is to understand the technology thoroughly.
Welcome to wechat, like and support collection!