Python Object-Oriented 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.