Article Directory
1. Constructor
1. Member variable assignment problem
In the previous blog, the defined Python class Student:
class Student:
name = None # 姓名
age = None # 年龄
def info(self):
print(f"姓名 : {
self.name} , 年龄 : {
self.age}")
If you want to assign values to its member variables, you need to assign values to the two member variables name and age respectively,
s = Student()
s.name = "Tom"
s.age = 18
For the above 2 variables, 2 lines of code are written. If there are more than a dozen variables, then more than a dozen lines of code are needed to complete the operation of member variable assignment;
2. Introduction of construction method
Using the construction method, you can assign values to multiple variables at the same time in one line of code , which can reduce the amount of code;
Introduction to the construction method:
-
Constructor function name: The constructor of the Python class is
__init__()
the method, and the name of the methodinit
has two underscores before and after the word_
; -
Construction method call timing: when creating an instance object of a class, the construction method of the Python class will be automatically executed
__init__()
; -
Construction method parameters: When creating an object, if parameters are passed in, the parameters will be automatically passed to
__init__()
the construction method for use;
3. Code example - construction method
In the code below, the Student class contains __init__
constructors, and info
member methods;
__init__()
In the construction method, two parameters name and age are received and assigned to name and age member variables respectively;
In info()
the method, print the student's name and age members to the console;
Code example:
"""
面向对象 代码示例
"""
class Student:
name = None # 姓名
age = None # 年龄
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(f"姓名 : {
self.name} , 年龄 : {
self.age}")
s = Student("Tom", 18)
s.info()
Results of the :
D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py
姓名 : Tom , 年龄 : 18
Process finished with exit code 0
3. The constructor can define member variables at the same time
In the above code example, the name and age member variables are defined,
class Student:
name = None # 姓名
age = None # 年龄
These two member variables can be omitted;
The constructor also has another function, which is to define member variables and assign values to them;
def __init__(self, name, age):
self.name = name
self.age = age
In the following code, no member variable is defined, and __init__()
the value is assigned directly in the construction method;
Code example:
"""
面向对象 代码示例
"""
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(f"姓名 : {
self.name} , 年龄 : {
self.age}")
s = Student("Tom", 18)
s.info()
Results of the :
D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py
姓名 : Tom , 年龄 : 18
Process finished with exit code 0