Python object-oriented-1 method, constructor, python object-oriented
Class refers to the definition of a thing. It is an abstract concept.
An instance refers to a specific individual of such a thing. It is a specific thing.
For example:
"Person" is a class. James is a specific example of mankind.
The same is true in programming. You must first define a "class". When you need it, use the "class" definition to create a specific example.
Creating an instance using the class definition is called class instantiation.
The instantiation process is to call the class constructor to complete the data allocation of the instance.
The following is an example of a washing machine:
This is a simple manual washing machine w instantiated The cycler class:
1 # class extends Er: 2 class extends Er: # define a extends er class 3 4 def _ init _ (self): # This is the constructor that defines the class, it is also a special instance method 5 self. water = 0 # instance attributes (variables) 6 self. scour = 0 7 8 def add_water (self, water): # instance method (function). parameters passed after instantiation can be accepted for instance variables in the class, because the instance inherits the attributes and methods of the class, 9 # is equivalent to 10 print ('add water: ', water) 11 self. water = water # reference instance variables in class methods using self. attribute name 12 13 def add_scour (self, scour): 14 self. scour = scour15 print ('add scour: ', scour) 16 17 def start_wash (self): 18 print ('start wash... ') 19 20 if _ name __= =' _ main _ ': 21 w = ER Er () # class instantiation 22 w. add_water (10) # pass the parameter to the instance variable 23 w. add_scour (2) 24 w. start_wash ()
Execution result:
To define an instance method in a class, you must manually add the self parameter (for example, 4th rows). However, you do not need to manually add the self parameter when calling this method after class instantiation. python will automatically add the parameter, for example, 22nd rows.
The instantiation method is w = Washer (). We can see that a bracket must be added after the class.
The instance method defined in the call class outside the class can be Instance name. Method Name (), for example
W. add_scour (2). If necessary, add the parameter.
Next, let's change the program to a semi-automatic washing machine, that is, the methods are called each other within the class.
Washa. py:
1 class runner Er: 2 3 def _ init _ (self): 4 self. water = 0 5 self. scour = 0 6 7 def set_water (self, water): 8 self. water = water 9 self. add_water () 10 11 def set_scour (self, scour): 12 self. scour = scour13 self. add_scour () # Call a function in the class and use self. method 14 15 def add_water (self): 16 print ('add water: ', self. water) 17 18 def add_scour (self): 19 print ('add scour: ', self. scour) 20 21 def start_wash (self): 22 print ('start wash... ') 23 24 if _ name __= =' _ main _ ': 25 w = ER Er () 26 w. set_water (10) 27 w. set_scour (2) 28 w. start_wash () 29
You can also change it:
1 class Washer: 2 3 def __init__(self): 4 self.water=0 5 self.scour=0 6 7 def set_water(self,water): 8 self.water=water 9 10 def set_scour(self,scour):11 self.scour=scour 12 13 def add_water(self):14 print('Add water:',self.water)15 16 def add_scour(self):17 print('Add scour:',self.scour)18 19 def start_wash(self):20 self.add_water()21 self.add_scour()22 print('Start wash...')23 24 if __name__=='__main__':25 w=Washer()26 w.set_water(10)27 w.set_scour(2)28 w.start_wash()
Running result:
You can do the following without setting the amount of water and detergent in the washing machine:
1 class runner Er: 2 3 def _ init _ (self): 4 self. water = 10 # change the instance attribute to the default value required by the standard laundry program 5 self. scour = 2 6 7 def set_water (self, water): 8 self. water = water 9 10 def set_scour (self, scour): 11 self. scour = scour 12 13 def add_water (self): 14 print ('add water: ', self. water) 15 16 def add_scour (self): 17 print ('add scour: ', self. scour) 18 19 def start_wash (self): 20 self. add_water () 21 self. add_scour () 22 print ('start wash... ') 23 24 if _ name __= =' _ main _ ': 25 w = ER Er () 26 # w. set_water (10) # comment out the two lines of code, indicating that the user does not need to set the amount of water and detergent 27 # w. set_scour (2) 28 w. start_wash () 29
In this case, only 10L water and 2L detergent can be added. Constructors are used.
1 class Washer: 2 3 def __init__(self,water=10,scour=2): 4 self.water=water 5 self.scour=scour 6 7 def set_water(self,water): 8 self.water=water 9 10 def set_scour(self,scour):11 self.scour=scour 12 13 def add_water(self):14 print('Add water:',self.water)15 16 def add_scour(self):17 print('Add scour:',self.scour)18 19 def start_wash(self):20 self.add_water()21 self.add_scour()22 print('Start wash...')23 24 if __name__=='__main__':25 w=Washer()26 w.start_wash()27 28 wb=Washer(100,10)29 wb.start_wash()30 31 wc=Washer(100,10)32 wc.set_water(50)33 wc.set_scour(5)34 wc.start_wash()35 36 37 38 39
Running result:
The constructor can enrich the instance objects of classes during instantiation and can instantiate different types of instances.