Definition of a method of a class
1.def fun_name (self,...);
Pass
2. The parameter self represents an instance of the class that is automatically supplied by the system when the method is called
3. The method definition must indicate the self parameter
The invocation of a method of a class
Similar to normal function calls
1. Internal invocation of the class:self.< method name > (parameter list).
2. Call the:< instance name >.< Method name > (parameter list) outside the class.
Note: In both of these invocation methods, self is not included in the supplied argument list.
To demonstrate a class:
Wash.pyclass Washer: def init (self): self.water = 0 self.scour = 0 def add_water (self,water): Print (' Add water: ', water) Self.water = Water def add_scour (self,scour): self.scour = scour print (' ADD scour: ', Self.scour) def start_wash (self): print (' Start wash ... ') if name = = ' main ': W = washer () W.add_water (W.add_scour) ( 2) W.start_wash ()
The running result of the program is:
To modify a program:
Washa.pyclass Washer: def init (self): Self.water = ten Self.scour = 2 def set_water (self,water): Self.water = Water def set_scour (self,scour): self.scour = Scour def add_water (self): print (' Add Water: ', self.water) def add_scour (self): print (' Add scour: ', Self.scour) def-Start_wash (self): Self.add_water () self.add_scour () print (' Start wash ... ') if name = = ' main ': W = washer () w.set_ Water (W.set_scour) ( 4) W.start_wash ()
The running result of the program is:
Intra-class methods call each other
1. Between the internal methods of a class can be called accordingly
2. Call the method with the method described above in the inside of the class
Construction method and its function
1. The construction method is the _init_ () method mentioned and used in the previous course.
2. The purpose of the constructor method is to initialize the instance when the class is instantiated.
The 3._init_ () method is the function that is called automatically by the first step of the class instantiation.
4. Note that the method name is fixed, but its parameters are the same as the normal method, at least with the self parameter.
5. Initialize instances include: Define and initialize instance properties: Or invoke some methods of the class.
6. The construction method can carry various parameters except self (keyword parameter, default parameter, collect parameters with tuple, collect keyword parameters in dictionary, etc.); When you instantiate a class, you pass in the specified value for the corresponding property.
Program Demo:
Washb.pyclass Washer: def init (self,water=10,scour=2): self.water = water Self.scour = Scour def set_ Water (self,water): self.water = water def set_scour (self,scour): self.scour = Scour def add_water ( Self): print (' Add water: ', self.water) def add_scour (self): print (' Add scour: ', Self.scour) def Start_wash (self): self.add_water () self.add_scour () print (' Start wash ... ') if name = = ' main ': # W = washer () # W.start_wash () WB = Washer (100,10) Wb.set_water ( 5) Wb.start_wash ()
The running result of the program is:
"Recommended"
1. Python class inheritance Explained
2. Parsing instance code for dynamic modification of Python classes
3. Detailed examples of Python class analysis
4. Detailed description L The Python class inherits
5. Introduction to Python class methods and Object methods