Python Object-oriented advanced

Source: Internet
Author: User

  Property built-in adorner functions are used only in object-oriented

 fromMathImportPiclassCircle:def __init__(self, R): SELF.R=R @property#Some methods that have similar properties that require manipulation are disguised as object properties    defperimeter (self):return2 * pi *SELF.R @propertydefArea (self):returnPI * SELF.R * * 2Circle= Circle (5)Print(Circle.perimeter)#calling methods and manipulating properties are essentially the same methodPrint(Circle.area)

   Property provides viewing, modification, and deletion of private properties

classPerson :def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name@name. Setter#modify by assigning values to modify    defname (self, new_name): Self.__name=new_name @name. Deleterdefname (self):delSelf.__nameP= Person ('Zxc')Print(P.name)#ZxcP.name ='ZXF'Print(P.name)#ZXFdelP.name#here del just corresponds to the @name.deleter, mainly see @name.deleter the following method to perform what#print (p.name) # Error shows no Name attribute

  Classmethod and Staticmethod

classPerson :__key='Human'    def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name@classmethod#turn a method into a method in a class so that the method can be called directly by the class without relying on the object    defChange_key (CLS, New_key): CLS.__key=New_keyPrint(Person._person__key)#HumanPerson.change_key ('Human')Print(Person._person__key)#Human#Classmethod should be used to decorate this method when the operation of this method involves only static properties.
Classmethod
classLogin:def __init__(Self,name,password): Self.name=name Self.pwd=PassworddefLogin (self):Pass@staticmethod#decorate a static method that does not have a relationship with a class or object, called by the class    defGet_usr_pwd ():#Static Methodsusr = input ('User name:') PWD= Input ('Password:') Login (usr, pwd)#generally used in fully object-oriented programming
Staticmethod

Python Object-oriented advanced

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.