Tag:python
Category:Python learning
Article From:https://www.cnblogs.com/xiaoyafei/archive/2018/04/08/8744954.html

Definition class

Define a class as follows:

class Class names:

  Method list

 

demo:Define a Car class

In [1]: #Define a Car class

In [2]: class Car():
   ...:     #Method
   ...:     def getCarInfo(self):
   ...:         print('The number of the wheels is:%d, the color is:%s'%(self.wheelNum,self.color))
   ...:     def move(self):
   ...:         print('The car is moving...')

Explain:

  1.There are 2 kinds of classes that define the class, the new type and the classic class, the Car above is the classic class, and the Car (object) is a new type.

  2.The naming rule of the class name is “the big hump”

create object

Just as we’ve defined a Car class, it’s like we have a car drawing, then we need to give it to the production workers to create it.

In Python, you can create a one object based on the class that has been defined, and the format of the object is to be created:

  Object name = class name ()

Create object Demo:

In [1]: #Define a Car class

In [2]: class Car():
   ...:     #Method
   ...:     def getCarInfo(self):
   ...:         print('The number of the wheels is:%d, the color is:%s'%(self.wheelNum,self.color))
   ...:     def move(self):
   ...:         print('The car is moving...')
   ...:         

In [3]: #Create an object and use the variable BMW to save its reference

In [4]: BMW =  Car()

In [5]: BMW.color = 'black'

In [6]: BMW.wheelNum = 4

In [7]: BMW.move()
The car is moving...

In [8]: print(BMW.color)
black

In [9]: print(BMW.wheelNum)
4

Summary:

  1)BMW = Car(),This produces an instance object of the Car, which can also be accessed by the instance object BMW at this time.

  2)The first time BMW.color = ‘Black’ is used to add properties to the BMW object, and if BMW.color = XXX appears again later, it represents a modification to the property

  3)BMWIt is an object that has attributes (data) and methods (functions)

  4)When creating an object, it is a kind of object made with a mold.

__init__Method

In the last section of the demo, we have to add the BMW object 2 properties, wheelNum and color, if create an object, would also like to add attributes to its, obviously this is very troublesome, so you need to have a method, when creating objects, ShunSet the attribute of this object

__init__Method

<1>Usage method

Class Class names:
    #Initialization function, which is used to complete some default settings
    def __init__(self):
        pass

<2>__init__Method call

 11 #Defining the car class
 12 class Car():         
 13                      
 14     def __init__(self):                                                                             
 15         self.wheelNum = 4
 16         self.color = 'black'
 17     def move(self):  
 18         print('The car is running...')
 19                      
 20 #create object
 21 BMW = Car()          
 22 print('The color of the car is:%s'%BMW.color)
 23 print('The number of car tires is:%d'%BMW.wheelNum)

Summary:

  When the Car object is created, the BMW acquiesce 2 attributes by default without invoking the __init__ () method. The reason is that the __init__ () method is called by default immediately after it is created.

think about it

Now that the __init__ () method has been executed by default after creating the object, is there any way to let the object pass some parameters when it calls the __init__ () method? If you can, how do you pass it?

 11 #Defining the car class
 12 class Car():
 13  
 14     def __init__(self,wheelNum,color):
 15         self.wheelNum = wheelNum
 16         self.color = color
 17     def move(self):
 18         print('The car is running...')
 19  
 20 #create object
 21 BMW = Car(4,'white')                                                                                 
 22 print('The color of the car is:%s'%BMW.color)
 23 print('The number of car tires is:%d'%BMW.wheelNum)

python3 test.py
The color of the car is: white
The number of car tyres is: 4

Summary:

  1)__init__()Method, when an object is created, it is called by default, and no manual calls are required

  2)__init__(self)The default is 1 parameters, the name is slef. If 2 objects are passed when the object is created again, then __init__ (self) needs 2 parameters besides self as the first parameter, for example, __init__ (self, x, y).

  3)__init__(self)The self parameter in the python does not need to be passed by the developer, and the current object reference is automatically passed in by the interpreter

Magic method

First, look at the following code:

 11 #Defining the car class
 12 class Car():
 13      
 14     def __init__(self,wheelNum,color):                                                              
 15         self.wheelNum = wheelNum
 16         self.color = color
 17     def move(self):
 18         print('The car is running...')
 19      
 20 #create object
 21 BMW = Car(4,'white') 
 22 print(BMW)    #This line of code is the output of BMW using print23 print('The color of the car is:%s'%BMW.color)
 24 print('The number of car tires is:%d'%BMW.wheelNum)

The results obtained:

python3 test.py
<__main__.Car object at 0x7f2b87338940>   #What you see here is the address of the created BMW object in memory
The color of the car is: whiteThe number of car tires is:4

__str__()Method

 11 #Defining the car class
 12 class Car():        
 13                     
 14     def __init__(self,wheelNum,color):
 15         self.wheelNum = wheelNum
 16         self.color = color
 17     def move(self): 
 18         print('The car is running...')
 19                     
 20     def __str__(self):
 21         return "The color of the car is:%s, the number of tires is:%d"%(self.color,self.wheelNum)                          
 22                    
 23 #create object
 24 BMW = Car(4,'white') 
 25 print(BMW)          
 26 print('The color of the car is:%s'%BMW.color) 
 27 print('The number of car tires is:%d'%BMW.wheelNum)

Summary:

  1)In Python, if the name of the method is __xxxx__ (), then there is a special function, so it is called the “magic” method

  2)When the print output object is used, as long as you define the __str__ (self) method, the data from the return in this method will be printed.

Understand self

Look at the following examples:

 11 #Define a class
 12 class Animal():      
 13             
 14     #Method
 15     def __init__(self,name):         
 16         self.name = name             
 17             
 18     def printName(self):             
 19         print('The name is:%s'%(self.name))
 20             
 21 #Define a function
 22 def myPrint(animal): 
 23     animal.printName()
 24             
 25 dog1 = Animal('Wangcai')
 26 myPrint(dog1)
 27 dog2 = Animal('Blessing')
 28 myPrint(dog2) 

Running results:

Name: WangcaiName: lucky

Summary:

  1)The so-called self, can be understood as oneself

  2)Self can be understood as the this pointer inside the C++ class, which is the meaning of the object itself.

  3)When an object calls its method, the Python interpreter will pass the object as the first parameter to self, so developers only need to pass on the following parameters.

Similar Posts:

Link of this Article: 12- object – oriented 1

Leave a Reply

Your email address will not be published. Required fields are marked *