1. Fields:
Static fields: Class fields, accessed by classes, have been created when the code is loaded.
Normal Fields: Object fields, accessed by objects, are generated when the object is created.
2. Method:
All methods belong to the class
Common method: At least one self, accessed by an object
Static methods: @staticmethod modifiers, arbitrary arguments, called by the class, not related to the object
Class method: @classmethod modifier, with at least one parameter CLS, class method is more than static method function, that is, the class name is automatically passed to the CLS
3. Properties:
A nondescript thing, with a method of writing form, with the Access form of a field
classPager:def __init__(Self,total_pager): Self.total_pager=Total_pagerdefdiv_page (self): A1,A2= Divmod (self.total_pager,10) ifA2 = =0:returnA1Else: returnA1+1P= Pager (102) ret=p.div_page ()Print(ret)
Execution Result: 11
If we are going to do a paging, in the traditional way, then write the above look, if changed to the property of the way, then the call, the execution of the time do not need parentheses
classPager:def __init__(Self,total_pager): Self.total_pager=total_pager @property defdiv_page (self): A1,A2= Divmod (self.total_pager,10) ifA2 = =0:returnA1Else: returnA1+1P= Pager (102)ret = p.div_page Print(ret)
For a field, we can get it through p.total_pager, or it can be set by p.total_pager=103, so how does the property be set in this way?
classPager:def __init__(Self,total_pager): Self.total_pager=Total_pager @propertydefdiv_page (self): A1,A2= Divmod (self.total_pager,10) ifA2 = =0:returnA1Else: returnA1+1@div_page. Setterdefdiv_page (self,value):Pass#p = Pager (102)#ret = P.div_page#print (ret)Print(P.total_pager) P.total_pager= 103Print(P.total_pager) p.div_page= 100
Do not report no attribute errors when executed.
Similarly, for a field we can perform a delete operation, so what about the property method?
classPager:def __init__(Self,total_pager): Self.total_pager=Total_pager @propertydefdiv_page (self): A1,A2= Divmod (self.total_pager,10) ifA2 = =0:returnA1Else: returnA1+1@div_page. Setterdefdiv_page (self,value):Pass @div_page. deleter def div_page (self): pass defShow (self):Print("HELLO") P= Pager (102)Print(P.total_pager) P.total_pager= 103delP.total_pagerdel p.div_pagep.show ()
Python Object-Oriented 2