The data types we learned earlier are classes, and we can use many methods in classes.
The definition of a class uses the class keyword , and the syntax structure is as follows:
className:''''Class Document'''content
Name is the name of the class we want to create. Please note that we try to use capital letters when using it. If we use two words to name it, the first letters of the two words must be capital letters.
Class documentation is information written for us to help us understand this class.
Content is the class body in the class, including variables, methods, attributes, etc. If we do not need to add content temporarily when defining the class, we can use the pass statement to act as a placeholder statement.
Let's define a class:
classSchool: #In this way we define a class named School'''Define a school class'''passschool=School()#In this way we call this class print(type(school))#Pass output Take a look at its type print(school)
The output is:
<class'__main__.School'><__main__.Schoolobjectat0x03372DD8>
From the results, we can see that school is a class, and from the second line, we can see that school is an instance of the School class.
When creating a class, a __init__() method is often created. This method is a construction method, that is, this method will be used every time the class is called, and parameters are also passed in this method.
This method must contain a self parameter, which must also be the first parameter. This parameter points to the instance itself, and it can access the properties and methods that exist in this class.
Look at the following example:
classSchool: #In this way we define a class. If we want to call this class'''define a school class''' def__init__(self):print('Here is the __init()__ method')def__judge__( self):print('Judge whether this method is called')school=School()
The output is:
Here is the __init()__ method
From the output, we can see that as long as this class is called, the __init__ method is automatically called, but the following __judge__ method is not called.
We can also use a loop to verify whether the init method will be used every time this class is called:
Look at the code below:
classSchool:'''Define a school class'''count=0def__init__(self):School.count+=1print('This is the %dth use of the __init()__ method'%School.count)foriinrange(5 ):School()
The output is:
Here is the first use of the __init()__ method. Here is the second use of the __init()__ method. Here is the third use of the __init()__ method. Here is the __init()__ The 4th use of the method Here is the 5th use of the __init()__ method
We can see from the output that we use the init method 5 times. When we call this class for the first time, the count is 0, and then the init method is added by 1. The school.count in the init method is increased each time. 1. Then the outermost count is unchanged. Through this example, we can learn how to use init.
When we are learning functions, the parameter transfer is defined in the function name, and in the class, we can more conveniently transfer parameters through the init method.
Look at the following example:
classGetAge:def__init__(self,name,age):self.name=nameself.age=ageprint('%s’s age is %d'%(name,age))GetAge('Xiao Ming',18)
The output is:
Xiao Ming’s age is 18
We need to note that self is essential and needs to be placed first. Then when we call this class, the number of parameters (excluding self) contained in the init method must be equal to the number we are calling. The actual number of parameters passed.