field | Method | Properties | Members of the Class | Python

Source: Internet
Author: User

#-----------------------------------------FieldclassClass:#static fields, belonging to class    #has been created and stored in the class, not every creation of an object created once;Country ='China'    def __init__(self, name):#normal field-->> saved in the created objectSelf.name =name#Common method-->> saved in class    defShow (self):Print(Self.name)#instantiating an objectobj= Class ('Mic')Print(Obj.name) obj.show ()#getting static fields from class objectsPrint(class.country)#print (class.name) # Normal fields are stored in the object and cannot be accessed through the class;#objects can be accessed to static properties through class object pointers (references)Print(Obj.country)
#------------------------------Methodclassmethodtest ():#Common Methods    defTest1 (self):#Self refers to the created object        Print('run-------1')    #Static Methods@staticmethoddefTest2 ():#Self is not necessary at this time, of course, if you write, you need to pass in a parameter        Print('run-------2')    #class Method@classmethoddefTest3 (CLS):#The CLS parameter passed in here is the class name that Python automatically passes in        Print(CLS)Print('run-------3')#-----------------Call the normal method#1. Create an object that accesses the normal methods in the class indirectly through the objectobj =methodtest () obj.test1 ( )#2. Direct access to the normal method, but only if an object is created first#because the first argument of the normal method is self, it refers to the created objectmethodtest.test1 (obj)#------------------Call a static method#class is called directly, similar to writing a function, after all, creating an object consumes the contentMethodtest.test2 ()#----------------Calling class methodsmethodtest.test3 ()#when do you use them separately?#1. We need to use the value in the object, the common method is used;#2. If you do not need the values in any objects, use static methods;#3. In fact, if you need to use the class name in a static method, passing in as a parameter is equivalent to a class method;#that is, a class method can be constructed entirely by a static method.#and there is little need to use the class name in the method, so it is not necessary for the class method
#------------------------------PropertiesclassAttr (): @propertydeftest4 (self):Print('run-------4')        return 'Run4'@test4. Setterdeftest4 (self, param):Print(param) @test4. deleterdeftest4 (self):Print('Delete------4') obj=Attr ()#obj.test4 () # errorObj.test4Print(OBJ.TEST4)#Assign ValueObj.test4 ='Change_val'#print (OBJ.TEST4) # error#What if you must assign a value?Print(OBJ.TEST4)#DeletedelObj.test4#>>> doesn't actually modify this field,#only through such operation, the corresponding adorner method is executed;#is a correspondence relationship;
#-----------------using the property decorator to complete the paging effectclassPaginator (object):def __init__(self, current_page):Try: P=Int (current_page)exceptException as E:p=1Self.page=P @propertydefStart (self): Val= (self.page-1) * 10returnVal @propertydefEnd (self): Val= self.page*10returnVala=list () forIinchRange (1000): A.append (i) whiletrue:p= Input ('Please enter the page you are viewing:') obj=paginator (P)Print(A[obj.start:obj.end])
#---------------------------Use property () to complete the above effectclassClassfunc ():deftest1 (self):return 'Mic'    deftest2 (self, param):Print(param)deftest3 (self):Print('del') per= Property (Fget=test1, Fset=test2, fdel=test3) obj=Classfunc ()#1. Calling Methods#ret = Obj.per#print (ret)#2. Assigning Values#obj.per = ' Mic '#3. DeletedelObj.per

field | Method | Properties | Members of the Class | Python

Related Article

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.